Opening New Windows

By Roger Johansson on Dec 4, 2006


When using strict doctypes the target attribute is not allowed for opening new windows. This script will accomplish that task easily and unobtrusively.

Good JavaScript site!


The script uses the class name “non-html” so you will need to add that to the links you want to open in a new window, e.g., <a href="yourDocument.html" class="non-html">A sample file</a>

This script will not open a new window if a modifier key (ctrl/shift/etc.) is pressed while the link is clicked.

It also checks if the new window was successfully opened. If it was not, it returns true to allow the browser to open the link in the original window.

Source Code

Paste this source code into the designated areas.

External file

Paste this code into an external JavaScript file named: openNewWindows.js


Paste this code into the HEAD section of your HTML document.


Paste this code into the BODY section of your HTML document