We ran into a usecase where a user has to explicitly target a tab in ie. For example users have logged into salesforce.com cloud in one window. These users are then taking calls in that window. However same users are using another internet application to complete their work. It would be messy to open up these multiple windows every time work needs to be done.

It is probably better to target just one window and the user transferred to one window. Better yet to target a specific tab.

So started my search. I thought it ought to be simple. As it turned out there doesnt seem to be a good way to do this through regular unsigned javascript in ie7. I did find a way to do this in firefox.

ie seem to control tab vs new window purely through user configuration. Users can choose to set a global ie option to open new windows targeted by "_blank" to either an actual new window or a tab. Once they set this option as tab then the simple window.open(url) will open that url in a new tab.

I still want to force to open in a new tab. Perhas I will find a way through signed javascript. that is for another day or week.

If you are further curious what follows is my research and notes on how I came to this conclusion.

How to target an ie tab from javascript

Search for: How to target an ie tab from javascript

May be there is something here. not sure. read later

is there a way to configure or precreate tabs in ie

Search for: is there a way to configure or precreate tabs in ie

open a link in a new tab larry williams

Search for: open a link in a new tab larry williams

perhaps here

An interesting read (no solution yet)

using javascript instead of target to open new windows

can i get a list of ie tabs through javascript

Search for: can i get a list of ie tabs through javascript

tabs parent and child windows in ie

Search for: tabs parent and child windows in ie

A general user level introduction to how tabs behave in ie7

ie7 behavior from the developers mouth

Not sure how one might use this but here is a ref to IWebBrowser2 ie interface

The ie7Blog

Jscript blog

jquery targeting tabs

Search for: jquery targeting tabs

you may want to read up on ie automation

ie access to browser object from javascript

Search for: ie access to browser object from javascript

javascript access to "tab" objects

Search for: javascript access to "tab" objects

list all windows open in javascript

Search for: list all windows open in javascript

A nice summary of windows using javascript


<html>
<head>
<script>
function say()
{
	alert('hello');
}
var nw = null;
function newwindow()
{
	nw = window.open();
}
function closewindow()
{
	nw.close();
}
function gotowindow()
{
	nw.focus();
}
</script>
</head>
<body onload="javscript:say()">
<p>hello</p>
<form>
<input value="OpenWindow" type="button" onClick="javascript:newwindow()"/>
<input value="CloseWindow" type="button" onClick="javascript:closewindow()"/>
<input value="focus" type="button" onClick="javascript:gotowindow()"/>
</form>
</body>
</html>

opener
or
window.opener

Documentation on open method


var nw = null;
function newwindow()
{
	//this works
	//load into a new unnamed window
	nw = window.open("http://www.google.com","_blank");

	//this works
	//load into a new named window
	//allows for "target" spec
	//nw = window.open("http://www.google.com", "mywindow");
	
	//but this does not work
	//not sure what the space is doing
	//nw = window.open("http://www.google.com", "my window");

	//this works
	//creates a window that doesnt go anywhere	
	//nw = window.open("about:blank");
	
	//this works
	//creates an "about:blank"
	//nw = window.open();
}

<html>
<head>
<script>
function say()
{
	//alert('hello');
}

var nw = null;
function newwindow()
{
	//this works
	//load into a new unnamed window
	nw = window.open("http://www.google.com","_blank");

	//this works
	//load into a new named window
	//allows for "target" spec
	//nw = window.open("http://www.google.com", "mywindow");
	
	//but this does not work
	//not sure what the space is doing
	//nw = window.open("http://www.google.com", "my window");

	//this works
	//creates a window that doesnt go anywhere	
	//nw = window.open("about:blank");
	
	//this works
	//creates an "about:blank"
	//nw = window.open();
}

function createwindow()
{
	if (isValidWindow() == false)
	{
		newwindow();
		return;
	}
	alert("A window is already created. use focus button to go there.");
}

function closewindow()
{
	if (nw == null)
	{
		alert ("Thsi window is not created. Click on open window to create one.");
		return;
	}
	if (nw.closed == true)
	{
		alert("window is already closed");
		return;
	}
	nw.close();
}

function isValidWindow()
{
	if (nw == null)
	{
		return false;
	}
	if (nw.closed == true)
	{
		return false;
	}
	return true;
}

function gotowindow()
{
	if (isValidWindow() == true)
	{
		nw.focus();
		return;
	}
	alert("sorry no window was created prior or closed");
}
function gotoMS()
{
	if (isValidWindow())
	{
		//this seem to have a security restriction
		//nw.document.location="http://www.microsoft.com";
		
		//but this will work
		nw.navigate("http://www.microsoft.com");
		nw.focus();
	}
	alert("Invalid window. Create one by using Open to test it");
}
function gotoGoogle()
{
	if (isValidWindow())
	{
		//nw.document.location="http://www.google.com";
		nw.navigate("http://www.google.com");
		nw.focus();
		return true;
	}
	alert("Invalid window. Create one by using Open to test it");
}
</script>
</head>
<body onload="javscript:say()">
<h2>Welcome to new window tester</h2>
<form>
<p>This will open a new window and remembers its handle. 
If ie is set to open in tabs then it will result in a tab.
<p>
<input value="OpenWindow" type="button" onClick="javascript:createwindow()"/>
</p>

<p>
<p>This will close the opened window or tab as a result
<p>
<input value="CloseWindow" type="button" onClick="javascript:closewindow()"/>
</p>


<p>
<p>This will shift the focus to the window that was created.
<p>
<input value="focus" type="button" onClick="javascript:gotowindow()"/>
</p>

<input value="Go to Google" type="button" onClick="javascript:gotoGoogle()"/>
<input value="Go to Microsoft" type="button" onClick="javascript:gotoMS()"/>
</form>
</body>
</html>

You can test this by clicking on the link below

Or you can do a save as on that link to download that file to your local hard disk and try it from there.

This code works if the open window targets a new window and not a tab. Especially focus. The focus works like a charm for new windows. However it doesn't work for tabs.

The very first time a URL is open in a new tab, the focus shifts to that new tab. However any subsequent opens or navigations or focus changes are not honored for that tab.

The work around seem to be first you obtain a javascript window pointer by opening tab with the same name as before. This gives us a window pointer. use that window pointer to close the named tab. Then reopen the tab with the same name. This time being a new tab it will shift the focus.

Howver this seems to be slightly erratic in field tests. It works 99% of the time. It is not clear what the hiccup is.

Another thread I have started to see how to create ie plugins

To see if a plugin can address the problem more consistently.