2

Is it possible to detect if window exist in javascript by window name?

My situation is this:

I open a popup, or a new browser tab.

Then I refresh the window that opened popup/new tab.

So my code runs again in window opener so I cannot save reference to opened popup/tab there.

I could use cookie but I would need to remove cookie when I close popup/tab, and I dont think there is a way to detect this, that the popup/tab has closed (cross browser of course, and ios!)

If I could save a popup/tab window name reference then I could check in window opener if popup/tab I have opened still exist?

Thank you!

Toniq
  • 4,492
  • 12
  • 50
  • 109

1 Answers1

0

You should be able to keep a reference to the window that you just opened.

Example from MDN:

var windowObjectReference = window.open(strUrl, strWindowName[, strWindowFeatures]);

That article has some additional information on re-opening the same window, etc;

Edit: From the MDN article linked above:

If a window with the name strWindowName already exists, then strUrl is loaded into the existing window. In this case the return value of the method is the existing window and strWindowFeatures is ignored. Providing an empty string for strUrl is a way to get a reference to an open window by its name without changing the window's location. To open a new window on every call of window.open(), use the special value _blank for strWindowName.

so something like this should work:

var whandle = 'persistantWindow';
var w1 = window.open('http://yoururl.org/page1.html', whandle);     // <- open a window with a name
var w2 = window.open('http://yoururl.org/page2.html', whandle);     // <- by re-using the same name, the new url will be opened in the same window
jasonscript
  • 6,039
  • 3
  • 28
  • 43
  • Well I said if I refresh the window that opened popup/new tab. Then I dont have that reference any more. – Toniq Jan 20 '14 at 02:05
  • See update. From the same MDN article, it says that you can pass a window name variable to the `window.open` command. By using the same window name, you can pass new URLs to the window and it will open in the same window/tab – jasonscript Jan 20 '14 at 02:19
  • I am not sure if I dont understand what you are trying to say or you dont understand what I am trying to do. If the popup is already opened I do not want to run that code in parent window. In other words, let say my application creates a div in parent window when code runs. Then I open popup and create the same div in popup window and remove div from parent window. Now the popup window is opened. Now someone refreshes parent window and this window is going to create div again in itself. I do not want to a div to be created in parent window if the popup is opened! – Toniq Jan 20 '14 at 02:30
  • I would use 'onbeforeunload' to detect window close but it doesnt work in opera and ios unfortunately. – Toniq Jan 20 '14 at 02:43
  • @Toniq I see, I misread your question. I think if you close the parent window, there's no way of checking whether a window is open. This would be a security hole because a malicious page could check for the presence of an internet banking window or something – jasonscript Jan 20 '14 at 03:19
  • 1
    I found this similar question: http://stackoverflow.com/questions/9364807/javascript-reference-browser-window-by-name – jasonscript Jan 20 '14 at 03:20