Tuesday, July 7, 2009

How to Open a Link in a New Session with IE8

There are some occasions when you want to sign into the same site twice with two different user IDs.  For instance you might be developing a new web application and need to compare how the application looks if you’re an administrator vs. a non-admin so you want to sign in twice to compare the two side-by-side.

In IE7 or under, you could simply click a shortcut to start a new instance of IE and the new instance would have its own session, but in IE8, they changed things a bit when they introduced their new process model.  Now all the separate processes share the same session unless you explicitly tell IE to start a new session.

To do this, you open the file menu (press Alt if the main menu isn’t shown) and choose “New Session”.  This opens a new IE window with its own session.  As far as I know, the cookies are kept completely separate.

For those of you who like the command line, you could also run iexplore.exe (located in C:\Program Files\Internet Explorer by default) with the -nomerge parameter.  This also means that you can create a shortcut to start a new instance of IE with a new session.

What I wanted though was to be able to right-click a link and choose “Open in New Session”.  This is a little harder, but turned out to be easier than I thought.  I originally thought I would have to dust off my C++ skills and finally learn to use COM (which I’ve tried my best to avoid), but all I wound up needing was an html file and a registry entry.

First, create an html file.  You can simply right click the folder where you want to create the file and choose New → Text Document, then rename the file to something with an “htm” extension.  I used C:\Program Files\NewSession.htm, though you can use any name or path you want, just make note of it when you create the registry entry.

Then open the file in your favorite text editor (i.e. Notepad) and copy/paste the following:

<script language="javascript">
var a = external.menuArguments;
if(a && (a = a.event) && (a = a.srcElement) && (a = a.href))
{
var shell = new ActiveXObject("WScript.Shell");
shell.run("\"C:\\Program Files\\Internet Explorer\\iexplore\" -nomerge \"" + a + "\"");
}
</script>


Then save the file.




Note:  Modifying the registry can be harmful to your computer if you don’t know what you’re doing.  If you’re unsure of yourself or you simply want to be careful, it’s best to ask for help from a friend or associate with knowledge of computers.




Then start the registry editor by holding the Windows key and pressing R, typing regedit in the run dialog and clicking OK.  In the registry editor, navigate to HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt and add a new key named Open in New Session.  Click this new key then double-click the (Default) REG_SZ and change the Value data to the path to your html file (e.g. C:\Program Files\NewSession.htm) and click OK.  Then add a new DWORD (32-bit) value named Contexts and modify the value to set it to the hex value 20 (32 in decimal).



That’s it!  you can close the registry editor and restart Internet Explorer.  When you right-click a link, there will be a new option “Open in New Session”.  When you click this option, a new instance of Internet Explorer will open with the URL of the link you clicked.  This new instance will have a brand new session.