///Automatic New Window for External Links

Automatic New Window for External Links

Automatic New Window for External Links

Pop one set of JavaScript code into your web page and all external links will open a new browser window — for JavaScript-enabled browsers.

Note that some surfers are annoyed when a link automatically opens a new window. The thought is something akin to, “If I want to open a new window with the link, I can do it myself.” Their choice of whether or not to open a new window is removed.

If the price of irritating a few is acceptable for making it easier for others to return to your web site, then the JavaScript below can come in handy.

Put the JavaScript at the bottom of the page, near the ending </BODY> tag. All web page links need to be above the JavaScript so the JavaScript can see them when it runs.

When the JavaScript runs, all links are given the target=”_blank” attribute except:

Links to your own domains. (You specify the domain names in the JavaScript, the only customization that’s required.)

Links that already have a value in target=”______” attribute.

Links that do not contain a web page URL.

Here is the JavaScript:

<script type="text/javascript" language="JavaScript">

<!-- Copyright 2005 Bontrager Connection, LLC
// More info: "Automatic New Window for External Links"
// at http://willmaster.com/possibilities/archives/
// Also see http://BontragerConnection.com/
//
// List the domain names that shall not be put into new
// windows when the link is clicked. If you use both
// the www.example.com and example.com versions of
// your domain name, list them both. The domains need
// to be all one line between the quotation marks on
// the line below, the names separated with a comma.
// Do not specify http:// URLs, just domain names.

var Domains = "www.willmaster.com,willmaster.com";

////////////////////////////////////////////
// No other customizations are necessary. //
////////////////////////////////////////////
if(Domains.indexOf(" ") != -1) {
var splitarray = Domains.split(" ");
Domains = splitarray.join("");
}
Domains = Domains.toLowerCase();
var DomainsArray = Domains.split(",");
for(var i = 0; i < document.links.length; i++) {
if(document.links[i].hostname.length < 1)
{ continue; }
if(document.links[i].target.length > 0)
{ continue; }
var h = document.links[i].hostname.toLowerCase();
var makeNewWindow = true;
for(var ii = 0; ii < DomainsArray.length; ii++) {
if(DomainsArray[ii] != h)
{ continue; }
makeNewWindow = false;
break;
}
if(makeNewWindow == true)
{ document.links[i].target = '_blank'; }
}
//-->
</script>

You’ll see at about line 15 in the above JavaScript is where the domain name exceptions are listed, the domain names of links that shall not open a new browser window.

Simply list the domain names between the quotation marks, all one line, a comma between each domain name. If you use both www.example.com and example.com versions of your domain name, then list both.

Hopefully this will make maintenance a little easier for you. Once the JavaScript is in place, you can add and remove links as you please, and those that should open a new browser window automatically do so.

2010-05-26T16:53:55+00:00 March 11th, 2005|JavaScript|0 Comments

About the Author:

Leave A Comment