A More Intelligent Open in New Window Script


Recently, I needed to create a small JavaScript that would make links for documents and outside web sites open the links in a new window. However, any link from the web site that held the links needed to open in the same window. This was made a bit more complicated by the fact that the system that was holding the links (a third party web content management system) did not allow for the user to decide if they wanted the link to open in a new window when they created a new link.

Because I could edit the JavaScript that loaded when the page was loaded, the solution that I came up with was to call the below function in a function that was called onload. What this JavaScript does is go through all of the links on the page and any link that matches the criteria is changed to have a target of blank.

function AddTarget(){
	var TargetTag2 = document.getElementsByTagName("a");
	for(var a=0; a

Sometimes a simple JavaScript can save the day.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <div> <blockquote> <object> <embed> <img> <param>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Copy the characters (respecting upper/lower case) from the image.