Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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:

  1. Insert 1 line into the web page to make all links on the page open in a new window (except links within iframe tags).

  2. 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:

  1. The browser might have a setting to prevent new windows from opening.

  2. Third-party software might prevent new windows from opening.

  3. The Opera browser opens a new tab instead of a new window.

  4. 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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC