Opening new windows with JavaScript, version 1.1

Update: The technique described here is not ideal. Read why in New windows with JavaScript and the target attribute.

After considering the suggestions made in the comments on my article Using JavaScript instead of target to open new windows and thinking some more about it, I've made some changes to the script. For the full story, please read the original article if you haven't already.

The revised script differs from the original in two ways:

To enable link styling depending on the type of file being linked to, I have revised the markup to use an additional class name that matches the file type. This has the additional benefit of enabling link styling in browsers with no JavaScript support. Note that I haven't styled the link in the demo document.

Example markup:

<a href="javascript-target.pdf" class="non-html pdf">A sample PDF file</a>

Try it out in the JavaScript target demo document.

Here is the revised script, which should be included in your common.js or wherever you store your global JavaScript functions:

// Create the new window
function openInNewWindow(e) {
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
		return true;
	else {
		// Change "_blank" to something like "newWindow" to load all links in the same new window
		var newWindow ='href'), '_blank');
		if (newWindow) {
		if (newWindow.focus) {
		return false;
		return true;
// Add the openInNewWindow function to the onclick event of links with a class name of "non-html"
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {
		// Change this to the text you want to use to alert the user that a new window will be opened
		var strNewWindowAlert = " (opens in a new window)";
		// Find all links
		var objWarningText;
		var link;
		var links = document.getElementsByTagName('a');
		for (var i = 0; i < links.length; i++) {
		link = links[i];
		// Find all links with a class name of "non-html"
		if (/\bnon\-html\b/.test(link.className)) {
			// Create an em element containing the new window warning text and insert it after the link text
			objWarningText = document.createElement("em");
			link.onclick = openInNewWindow;
		objWarningText = null;

Either copy and paste from here or download my sample javascript-target.js file, which contains all the JavaScript you need to use this.

You also need to make sure the getNewWindowLinks() function is executed when the document has loaded. Use whichever addEvent() function you prefer using:

addEvent(window, 'load', getNewWindowLinks);

That line is included in the javascript-target.js file that accompanies this article.

If you have any suggestions for further improvement, please post a comment.

Update: I have updated the script again. More information is available in Opening new windows with JavaScript, version 1.2.

Posted on May 9, 2006 in JavaScript