It has been somewhat of a mystery to me how the JavaScript window.open()
command behaves. You will find several explanations on the internet that you should use things like:
window.open("www.yoururl.com", "_blank");
or
window.open("www.yoururl.com", "_newtab");
But in some cases your browser will simply refuse to open in a new tab and do it in a popup window. When working on my View in Staging and Live UI extension, I finally figured out how this behavior works, with thanks to this answer on Stack Overflow;
window.open()
will not open in new tab if it is not happening on actual click event.
If the window.open()
method is called on an actual click event, it should always open in a new tab, unless you set window size properties. This will of course only work if the user has appropriate settings in his browser.
So as a developer you most of the time have control over how you call this method, and thus how you want the window to be opened. In my mentioned extension, I was initially calling window.open()
in a event handler method, so window.open()
was called asynchronously and thus no matter what I used, it always opened in a popup window, rather than a tab. To solve this I have used the following code:
// open window on actual click event with about:blank page
var tab = window.open("", "some name");
// event handler
var onItemLoaded = function whatever$_execute$_onItemLoaded() {
$evt.removeEventHandler(item, "load", onItemLoaded);
// some code to determine the url from the loaded item
var url = siteUrl + item.path;
// open url in tab and set focus
tab.location.href = url;
tab.focus();
};
// load item asynchronously
$evt.addEventHandler(item, "load", onItemLoaded);
item.load(item.isLoaded(true), $const.OpenMode.VIEW);
Now that is the explanation why sometimes when you want to open a window in a tab, it can just open as a popup window. But on IE I have also seen a different behavior which I can only explain as a defect in IE. As the OP has mentioned, in most cases the window is opened as expected (in a popup window). So this code must use the following call (or something like it):
window.open("www.theurl.com", "window name", "width=500,height=600");
It should never, in any case, open in a new tab, as it is called with the specs
argument. But I have seen this behavior before (opening in a popup window once and in a tab later), and only on older IE browsers. So the only thing I can conclude from that, is that this is a defect in IE...