Variable Scope in html pages

K

Kevin Spencer

JavaScript variables are not scoped beyond the currnet document. Sorry you
don't want to pass it, but that's what you have to do.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
K

KenA

Hi. Suppose I have 2 html pages. In the first html page I have the following
code:

<form id="Form1">
<script type="text/javascript">
function MyObject()
{
this.DoSomething = function()
{
v1 = 'var1'; }
}

mo1 = new MyObject();
mo1.DoSomething();
//alert( v1 ); </script>
</form>

So, in the scope of the first html page I have v1 == 'var1'

Now suppose I open a second page ... How can I read v1 in the first html
page. I don´t want to 'pass' the variable to the second page like we can do
it in post methods, I just like to 'enter' the scope of page 1 from page 2,
is it possible?

Suppose I load the second page without unloading the first page, like a pop
up or via link with target blank?

»»» KenA.
 
F

FlyBoy

From http://www.developer.irt.org/script/165.htm

If one window opens another:

<script language="JavaScript"><!--
function newWindow(file,window) {
msgWindow=open(file,window,'resizable=no,width=200,height=200');
if (msgWindow.opener == null) msgWindow.opener = self;
}
//--></script>

<form>
<input type="button" value="Open New Window"
onClick="newWindow('a.html','window2')">
</form>


Then to access a variable in the new window from the original window:

var myVar = msgWindow.myVar;


To access a variable in the original window from the new window:

var myVar = opener.myVar;


To access the contents of a form field in new window from the original
window:

var myVar = msgWindow.document.formName.formFieldName.value;


To access the contents of a form field in the original window from the new
window:

var myVar = opener.document.formName.formFieldName.value;


To access the contents of a form field in the original window from a frame
in the new window:

var myVar = top.opener.document.formName.formFieldName.value;
 
M

MD Websunlimited

Hi Ken,

To accomplish you'd have to have the handle to the other window. This is contained in the window.opener property. From there you can
access any page global vars.
 
M

MD Websunlimited

Kevin,

If the second page was opened by the first you can use the window.opener property to access the windows global vars.
 
K

Kevin Spencer

Yeah, Mike. That one slipped my mind.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

MD Websunlimited said:
Hi Ken,

To accomplish you'd have to have the handle to the other window. This is
contained in the window.opener property. From there you can
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top