faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

61 of 83 people (73%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do I pass a variable to a new window, and use that varialbe in the new window?

Apr 5th, 2008 20:18
ha mo, sato eric, Scott Davis, Jean-Bernard Valentaten, Darton Williams,


> There are a couple of options. I'm assuming that the variables you 
> want to pass are text entered into a form, but even if they're 
> variables that are dynamically set by your script the following still 
> applies, because you can set the value of a hidden form field and get 
> the same effect.

> (1) You can pass the variable in the URL and parse it using 
> document.URL, which is a bit of a pain.

> (2) You can access the opener's form elements by doing the following:

> In the initial page (the opener), use a standard window.open

>      function openWin()
> 	     {
> 		myWindow = window.open
>('blah2.html', 'newWindow','width=400,height=200,'+
> 'status=no,toolbar=no,menubar=no');
>	     }

>and a form

>     <FORM name="myForm">
>     <input type="text" name="text1">
>     <input type="button" value="Open Window" onClick="openWin()">
>     </FORM>

>Then in the body of your new window (blah2.html here), use the 
>following syntax:

>     var textField = window.opener.document.myForm.text1.value;
>     document.write(textField);

> This same method can be used to set the value of a form field within 
> the new window, etc. This works in both Netscape and IE.

Or you could simply access the variable with the opener-property.
Let's say you have a var called myVar. It should be declared like this:

<script language="JavaScript">
  var myVar = 'I\'ve got something to say';
</script>

Now you open a new window (see above). In this window you can access 
the var myVar like this:

<script language="JavaScript">
  var myVar2 = opener.myVar;
</script>

That is the fastest and easiest way.

HTH

***
Killer, and thanks.  I just happend to figure out both last night after 
a late one.  I used both the form read (hidden) and var read 
(constant).  

The problem I was haing was that I wanted to read a VARIABLE not a 
CONSTANT.  For some reason, the only way I was able to get a VAR read 
was to use the FORM method.  It seems (maybe my syntax is off?) that 
when I passed my variable to a var before the window.open I had trouble 
reading that var.  I had much better luck with the form read.

### Here is what I wanted: (in psudo-code)
choose URL [a, b, c]; store choice; open new win (html_doc); access 
opener.choice from html_doc; display opener.choice as an image.

From what I read (thanks again) it appears that both methods, var and 
input (form) storage/retrieval should work.  However, like I said only 
the form method was working for me.  (again syntax?)  I will continue 
to try methods unitl I get what I am looking for.

Thanks again for the great answers!


See you out there...
Scott



But anyone of you know how to send a variable from new window to caller 
window?



http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com