Alan’s Mind


How to Open a Link in a New Session with IE8

Posted in Development by waissguy on July 7, 2009
Tags: , , , , ,

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.

Use JavaScript to get the position of the caret in a Text Box

Posted in Development by waissguy on May 1, 2009
Tags: , , ,

If you have an <input type=”text” /> on your web page and that element currently is in focus, you can use this cross-browser function to retrieve the current position of the caret/cursor:

function GetCaret(input)
{
	if(document.selection)
	{
		var range = document.selection.createRange().duplicate();
		//moveStart returns the # of characters moved.
		return -range.moveStart("character", -input.value.length);
	}
	else if(input.selectionStart)
		return input.selectionStart;
	return -1;
}

Internet Explorer doesn’t support the selectionStart property used by FireFox (I’ve heard Opera uses selectionStart and I believe Safari does as well) so instead we get the currently selected range via document.selection, use the selection object’s createRange method to create a TextRange object which is what IE uses to manipulate text data. There doesn’t seem to be a direct way to get the position of the TextRange within the parent object, but we can use the moveStart method to move the start point of the range. This method returns the number of characters moved, which happens to be the position of the caret – technically the negative of the position of the caret since you had to move the selection backwards to reach the start.

This method could easily be modified to get the ending position of the selection by using range.text in IE to retrieve the text selected (range.text.length would be the length of text selected) or use input.selectionEnd in other browsers.