15 4 / 2010

window.open behavior in Google Chrome

In Chrome,if window.open is called with same window name by pages which are managed by the same process, then the first call creates a new window and subsequent calls return reference to the existing window.If the pages are managed by different processes, then each call to window.open creates a new window.So far, i have been thinking

that if window.open is called with name of window which is already open, then a reference to that window is returned, instead of creating a new one. At least thats what MDC and MSDNsay.

From MDC,

If a window with the name strWindowName already exists, then, instead of opening a new window, strUrl is loaded into the existing window. In this case the return value of the method is the existing window.

Before diving into the details, lets have a quick Chrome process architecture primer. You are probably aware that Chrome has a multi process architecture. But contrary what you might think, not every tab has its own process. Some tabs reuse the existing process. So, how does Chrome decide to create a new process for a tab or use an existing one?

From Chrome webmaster FAQ

New tabs spawned from a web page, however, are usually opened in the same process, so that the original page can access the new tab using JavaScript.

The above statement holds good even if the tab spawned from a web page is of a different domain. So, watch your external links.The corollary of this statement is that if a user opens the same page from the same domain,in fresh tabs (using Ctrl-T or the ‘plus’ button), then those two pages reside in a separate process. Try these steps to see it in action.

  • Search  ”cnn” in Yahoo.com and open the first link using Ctrl - left click. It should open in a new tab.
  • Now, open another tab and type in cnn.com.
  • Open Chrome task manager, you can see 3 items, yahoo search results page and 2 CNN pages. Click on the yahoo search results item, it will also select one CNN.com tab. This means those 2 tabs are managed by the same process. The other cnn.com opened in a fresh tab is managed by a different process.

Chrome Task ManagerBack to our original discussion. This is going to sound like a math theorem, but i cant find a simpler way to explain it. Lets assume page P from domain D is loaded in tabs T1 and T2 and both are managed by different processes. On a button click, P opens a new window using window.open. First button click from P in T1 will open a new window and the first button click from P in T2 will open another window. Subsequent clicks in T1 and T2 will reuse their existing window(s) respectively. But the window created by T1 cannot be used by T2 and vice versa, even though they are from the same domain D.

In IE6, 7, 8 & Firefox, T2 can access the window opened by T1 and all clicks in T1,T2 reuse the same window.  The bottom line is Chrome creates 2 windows for a scenario where every other browser does only one. You can probably argue, Chrome does it right and 2 pages from same domain in separate processes shouldn’t be allowed to talk to each other.

Well, in IE6 every window is a new process, but that allowed 2 pages from same domain use the same popup window. Ok, I cannot win any argument these days by quoting IE6. But IE8 has a multi process architecture too. It does it right.

How does this affect you? It may not, but its good to know. Does this make me hate Chrome? Not at all.

Is this a bug or its intentional behavior? Any thoughts?