How to Force Offsite Links to Open in a New Window with User Scripts
Force Offsite Links to Open in a New Window
Keep your browser organized by automatically opening each site in its own window.
I originally wrote this user script after someone posted a request to the Greasemonkey script repository. I personally like to open links in a new tab in the current window, but some people prefer to open a separate window for each site. Offsite Blank lets you do this automatically, by forcing offsite links to open in a new window.
The Code
This user script runs on remote web sites (but not, for example, on HTML documents stored on your local machine that you open from the File Open menu). Since search engines exist to provide links to other pages, and I find it annoying for search result links to open new links, I've excluded Google and Yahoo! by default.
The code itself breaks down into four steps:
-
Get the domain of the current page.
-
Get a list of all the links on the page.
-
Compare the domain of each link to the domain of the page.
-
If the domains don't match, set the target attribute of the link so that it opens in a new window.
Save the following user script as offsiteblank.user.js:
// ==UserScript==
// @name Offsite Blank
// @namespace http://diveintomark.org/projects/greasemonkey/
// @description force offsite links to open in a new window
// @include http*://*
// @exclude http://*.google.tld/*
// @exclude http://*.yahoo.tld/*
// ==/UserScript==
var sCurrentHost = location.host;
var arLinks = document.links;
for (var i = arLinks.length - 1; i >= 0; i--) {
var elmLink = arLinks[i];
if (elmLink.host && elmLink.host != sCurrentHost) {
elmLink.target = "_blank";
}
}
Running the Hack
After installing the user script (Tools Install This User Script), go to http://www.fsf.org. Click on one of the links in the navigation bar, such as "About us." The link will open in the same window, as normal.
Go back to http://www.fsf.org, scroll to the bottom of the page, and click the Plone Powered link to visit http://plone.org. Since the link points to a page on another site, Firefox will automatically open the link in a new window, as shown in Figure 2-4.
Figure 2-4. Offsite link opened in a new window

Hacking the Hack
This hack is somewhat naive about what constitutes an offsite link. For example, if you visit http://www.slashdot.org and click a link on http://developers.slashdot.org, the script will force the link to open in a new window, because it considers developers.slashdot.org to be a different site than www.slashdot.org. We can fix this by modifying the user script to compare only the last part of the domain name.
Save the following user script as offsiteblank2.user.js:
// ==UserScript== // @name Offsite Blank 2 // @namespace http://diveintomark.org/projects/greasemonkey/ // @description force offsite links to open in a new window // @include http*://* // @exclude http*://*.google.tld/* // @exclude http*://*.yahoo.tld/* // ==/UserScript== var NUMBER_OF_PARTS = 2; var sCurrentHost = window.location.host; var arParts = sCurrentHost.split('.'); if (arParts.length > NUMBER_OF_PARTS) { sCurrentHost = [arParts[arParts.length - NUMBER_OF_PARTS], arParts[arParts.length - 1]].join('.'); } var arLinks = document.getElementsByTagName('a'); for (var i = arLinks.length - 1; i >= 0; i--) { var elmLink = arLinks[i]; var sHost = elmLink.host; if (!sHost) { continue; } var arLinkParts = sHost.split('.'); if (arLinkParts.length > NUMBER_OF_PARTS) { sHost = [arLinkParts[arLinkParts.length - NUMBER_OF_PARTS], arLinkParts[arLinkParts.length - 1]].join('.'); } if (sHost != sCurrentHost) { elmLink.target = "_blank"; } }
This script is still naive about what constitutes an offsite link; it's just naive in a different way than the first script. On sites such as http://www.amazon.co.uk, this script thinks the current domain is co.uk, instead of amazon.co.uk. You can further refine this behavior by changing the NUMBER_OF_PARTS constant at the top of the script from 2 to 3.