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
>and a form
> <FORM name="myForm">
> <input type="text" name="text1">
> <input type="button" value="Open Window" onClick="openWin()">
>Then in the body of your new window (blah2.html here), use the
> var textField = window.opener.document.myForm.text1.value;
> 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:
var myVar = 'I\'ve got something to say';
Now you open a new window (see above). In this window you can access
the var myVar like this:
var myVar2 = opener.myVar;
That is the fastest and easiest way.
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
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...
But anyone of you know how to send a variable from new window to caller