Use existing window in another tab
I have the main page (called 'main.html'), with a link on it, with which I can open a popup or focus the popup if already existing: var test; if (test == null || test.closed) test = window.open('test.html','test','width=800,height=600,location=0'); else test.focus();
Works fine, but here comes the problem: If the user opens the main page more than one time in his browser, clicks the link in main #1 and then clicks the link in main #2, it uses the existing popup, but reloads the site, because he doesn't know, that the popup is already opened.
Is t开发者_C百科here a way, that I can tell the other main windows the handle of the popup, so they can use it?
Thanks for your advise.
If the two "main windows" are opened independently from each other, there is no way for them to access the variables of each other.
You can however get a reference to the window "test" with window.open
, and then check if a variable you set in "test.html" is defined, to find out if it it's loaded:
var test;
if (!test || test.closed) {
test = window.open('','test','width=800,height=600,location=0');
if (test && !test.someVariable) {
test.location.href = "test.html";
}
} else
test.focus();
BTW, notice I changed the check in the first if from test == null
to !test
, because the variable will be originally undefined
and not null
, so that test == null
will result in false
.
This would need you to being able to access the main window in all the other tabs to check whether any of them have opened a popup. As far I know the only way to get a reference to a window is from the reference returned by the function window.open.
Not allowing javascript to accessing other windows and tabs except inside the window which opened the windows/tabs is due to security. Imagine if javascript could go through all your tabs and run functionality. That would be quite a big security risk.
精彩评论