[GLLUG] OT: JavaScript

Jeremy Bowers jerf at jerf.org
Thu Aug 10 15:51:55 EDT 2006


Dan Ryan wrote:
> This is off-topic, I know...but I am having the hardest time figuring
> this out!  Does anyone know some javascript?
> 
> I need an onclick function that passes variables from a main page to a
> popup.  I can't send it via a query string, since the popup window can't
> refresh itself(the window contains a flash player)  .....
> 
> .....
> 
> I'm so lost

You have two basic options.

The child window can access its 'location.search' parameter, and pull
any necessary data out of a querystring you used to open the window.
This is probably the best approach if you have to pass small amounts of
string or numeric data into the child window, because there are no
order-of-operation issues.

The other possibility is that when you open the new window, the return
of the window.open call is a reference to the new window object.
"Top-level" Javascript variables live on the window object, so you can
set attributes on this window object and they will appear in the new
window. This may have calling order issues, so you may have to poll in
the child window for when it has all the information by using a
"window.setTimeout" until some flag variable is set. This is trickier,
but is the only way to get real Javascript objects into that window that
I know. (You may not actually need to poll, but I try to simply avoid
needing to know how Javascript interpreters 'thread' across windows.)

(Odds are you aren't trying to pass objects around, though.)

To see the second trick in action, go to
http://www.jerf.org/resources/xblinjs/demo.html . In the Javascript
console, enter "w = window.open(location)". Return to that window, and
type "w.test = 1". Then, in the new window's Javascript console, type
"test" and hit enter, and you will see the 1 has travelled into the
child window.

Note you don't need to type "window.test" in the new window, because all
variable names are assumed to be on the "window" object. (This can be
useful to know.)


More information about the linux-user mailing list