Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if window is already open window.open

Tags:

I have a html page. In the body of the page I am calling onload event which calls javascript function to open a pop up window. here is the code:

var newWindow = null; function launchApplication() {     if ((newWindow == null) || (newWindow.closed))     {         newWindow = window.open('abc.html','','height=960px,width=940px');     } } 

when I move to another page, and come back to that page again, popup reopens, although it is already opened. Please guide me to proper direction so that if pop up is already open then it should not open again. I tried document.referred but it requires the site online, currently I am working offline.

like image 762
Saghir A. Khatri Avatar asked Aug 27 '12 08:08

Saghir A. Khatri


People also ask

How do I know if a popup window is closed?

Here is the code: var win = window. open('http://www.google.com', 'google','width=800,height=600,status=0,toolbar=0'); var timer = setInterval(function() { if(win. closed) { clearInterval(timer); alert('closed'); } }, 1000);

How do I know if my child's window is closed?

If you store a reference to the child window when you call window. open() , then you can poll using setInterval() to see whether the window is still open using the window. closed property.

Does window open create a new session?

When you open a page in a new tab or window, the web browser creates a new session. If you open multiple tabs or windows with the same URL, the web browser creates a separate sessionStorage for each tab or window.


1 Answers

newWindow = window.open('abc.html','com_MyDomain_myWindowForThisPurpose','height=960px,width=940px'); 

Give the window a name. Basing the name on your domain like this, prevents the chances of you picking a name someone else happened to choose.

Never make up a name that begins with _, those are reserved for special names the browser treats differently (same as with the "target" attribute of anchor elements).

Note that if the window of that name was opened with different options (e.g. different height), then it'll keep those options. The options here will only take effect if there is no window of that name, so you do create a new one.

Edit:

Note that the "name" is of the window, not of the content. It doesn't affect the title (newWindow.document.title will affect that, as of course will code in abc.html). It does affect other attempts to do stuff across windows. Hence another window.open with the same name will reuse this window. Also a link like <a href="def.html" target="com_MyDomain_myWindowForThisPurpose">clicky!</a> will re-use it. Normal caveats about browsers resisting window-opening in various scenarios (popup-blocking) apply.

like image 119
Jon Hanna Avatar answered Oct 28 '22 01:10

Jon Hanna