how to access a window variable from a popup window?
I already lost some hours trying to figure out how to solve a javascript problem. I have a JS function that opens a popup window:
function openSearchWindow(searchType, targetIdField, targetDescField)
and I set 2 properties to this new window:
var win = window.open(searchPage, searchType + "search", style);
win.targetIdField = targetIdField;
win.targetDescField = targetDescField;
Until here, everything works perfectly. In my popup window, however, I need to access these two variables I set previously: win.targetIdField and win.targetDescField
How can I access them? I tried almost everything.
EDIT - In time,
In the popup window I have a search engine actually. When user clicks in a result, there is a JS function that gets the ID and DESCRIPTION of the selected item and passes it back to the "opener" 开发者_运维百科window, placing them in the targetIdField and targetDescField, respectively.
EDIT (2)
Once the search is performed in the popup (through a servlet) the popup's URL changes, but it's always within the same domain.
Thanks in advance.
Lucas.
Have you tried accessing them through window.targetIdField
? If thait does not work, you could bind the 2 properties on the parent window (using window.targetIdField
instead of win.targetIdField
) and then access those from your opened window using window.opener.targetFieldid
.
I suggest tou use setAttribute and getAttribute, since this is supported by every browser (that I know of).
//Setting the properties (in the parent window)
win.setAttribute('targetIdField', targetIdField);
win.setAttribute('targetDescField', targetDescField);
//Accessing the properties (in the pop-up window)
window.getAttribute('targetIdField'); //you might need to use lowercase letters...
//If that doesn't work you can try multiple things
this.getAttribute...
win.contentWindow
will give you access to the new window object.
Then you can use:
win.contentWindow.targetIdField = targetIdField;
win.contentWindow.targetDescField = targetDescField;
To set targetIdField
and targetDescField
variables for your new popup's window object.
Note that cross-window access is restricted to the same domain.
Accessing a variable from parent window/opener window can be done by window.opener.yourVariableName
精彩评论