Opening New Windows
Many, if not most, site owners know how to make a link to open a new browser window. (I'll repeat it in this article, nevertheless.)
But, there is more to it.
This article shows how to:
Insert 1 line into the web page to make all links on the page open in a new window (except links within iframe tags).
Use JavaScript to make all links to other domains open new window.
The Basic "Open In New Window" Code
The target attribute for the HTML A (anchor) linking tag was developed when frames were developed. It tells the browser which frame to send the linked page to.
The format is: target="framename"
How To Use It To Open a New Window
If the name of the target is "_blank", browsers will open a new window for the linked page.
For most browsers, a target name (except "_self") that does not correspond to a current frame name will open a new window.
But that behavior can't be depended on. Use target="_blank" to open a new window.
Here is an example:
<a href="//www.willmaster.com" target="_blank"> Click for site development and automation food. </a>
There are some exceptions to the "'_blank' always opens a new window" statement. Here are ones I'm aware of:
-
The browser might have a setting to prevent new windows from opening.
-
Third-party software might prevent new windows from opening.
-
The Opera browser opens a new tab instead of a new window.
-
The Firefox browser can be set up to open a new tab instead of a new window.
Contrasting with the target="_blank" attribute, the target="_self" will open the linked page in the current window.
How To Automatically Open All Links In a New Window
There is one line of HTML code you can put into the HEAD area of your web page source code that will cause all links on the page to open a new window - except those that already have a target attribute in the anchor tag.
It's so much easier to paste one line of HTML into a page than it is to change every anchor tag.
Here is the line:
<base target="_blank">
It establishes a default target, which applies to all links on the page that don't already have a target attribute.
If you have links on the page that must open in the current window, you can override the default by putting the target="_self" attribute into those anchor tags.
Opening Only External Links In a New Window
If all links on a page that lead to another domain should open a new window, but links that lead to page on the same domain should load in the current window, don't use the BASE tag.
Instead, put this JavaScript at the bottom of the BODY area of the source code:
<script type="text/javascript" language="JavaScript"> <!-- Copyright 2006 Bontrager Connection, LLC // Code obtained from https://www.willmaster.com/library/ var thispagedomain = ExtractDomainName(document.URL); for(var i = 0; i <= document.links.length - 1; i++) { var url = document.links[i].href.toLowerCase(); if(url.indexOf('http://') != 0) { continue; } var hrefdomain = ExtractDomainName(url); if(thispagedomain != hrefdomain) { document.links[i].target = '_blank'; } } function ExtractDomainName(s) { var i = s.indexOf('//'); if(i > 1) { s = s.substr(i+2); } i = s.indexOf('/'); if(i > 1) { s = s.substr(0,i); } i = s.indexOf(':'); if(i > 1) { s = s.substr(0,i); } var re = /[a-z]/i; if(! re.test(s)) { return s; } var a = s.split('.'); if(a.length < 2) { return s; } var domain = a[a.length-2] + '.' + a[a.length-1]; if(a.length > 2) { if(a[a.length-2].length==2 && a[a.length-1].length==2) { domain = a[a.length-3] + '.' + domain; } } return domain.toLowerCase(); } //--> </script>
The JavaScript needs to be put into the web page source code somewhere below the last link. Right above the closing </body> tag would be good. That way, the JavaScript knows what it has to work with.
Placement of this doesn't matter with some browsers, but with others it does.
The JavaScript will automatically insert a target attribute value "_blank" into links leading off site.
Will Bontrager