Open External Links as Blank Targets via Unobtrusive JavaScript
Published Tuesday, November 20, 2007 @ 10:05 am • 11 Responses
Beginning with this article, I am serving up a short series of unobtrusive JavaScript functions that I use to enhance the functionality of Perishable Press. In this post, I present a comprehensive JavaScript method of opening external links in new windows (or tabs, depending on the browser).
One way of opening links in new windows is to insert the HTML target=”_blank” attribute into all necessary anchor elements (<a href=""></a>). This method works well, but generates validation errors when used with XHTML-Strict doctypes.
A better solution is to employ some unobtrusive JavaScript to progressively enhance your documents with “blank-target” functionality. Using the following code, 99% of your visitors (those with JavaScript) will enjoy external links opening in new windows, while the remaining 1% of your audience (those without JavaScript) will enjoy your site without even realizing they are missing out on those wonderful blank-targeted links. It’s a “win-win” situation ;)
To implement this unobtrusive, gracefully degradable strategy, simply replace any target="_blank" attributes with the XHTML-friendly rel="external" attribute. Using rel="external" is standards-compliant and thus completely valid, even for XHTML-Strict and XHTML-1.1 doctypes.
After you have prepared your external link anchors with rel="external" attributes, apply the following JavaScript either inline or externally. No additional editing or markup is required — grab, gulp, & go!
// Open External Links as Blank Targets via Unobtrusive JavaScript
// http://perishablepress.com/press/2007/11/20/open-external-links-as-blank-targets-via-unobtrusive-javascript/
function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
if (
anchor.getAttribute("href") && (
anchor.getAttribute("rel") == "external" ||
anchor.getAttribute("rel") == "external nofollow" ||
anchor.getAttribute("rel") == "nofollow external" )
)
anchor.target = "_blank";
}
}
window.onload = function() {
externalLinks();
}
Other JavaScript methods 1 for opening external links in new windows work only if the rel attribute is set to external. However, in many situations, especially when working with WordPress and its myriad plugins, external links are also tagged with additional properties, such as nofollow, for example. The code presented in this article opens all links with any of the following attributes in a new window (click links for demo):
Of course, by modifying and/or emulating the sequence of anchor.getAttribute() expressions, it is relatively (no pun intended) straightforward to trigger blank-target behavior for virtually any rel attribute.
Footnotes
- 1 This script has been adapted from the technique presented here.
About this article
Related articles
- The Friendliest Link Targets in the Neighborhood
- Unobtrusive JavaScript: Auto-Clear and Restore Multiple Form Inputs on Focus
- One Link to Open Them All
- Unobtrusive JavaScript Dynamic Clock
- Lightbox + FancyTooltips Bug Fix
- Hacking WordPress: Nofollow Blacklist for Commentator Links
- Hacking WordPress: Dofollow Whitelist for Commentator Links
Dialogue
11 Responses Jump to comment form
November 20, 2007 at 2:48 pm
Perishable: What if an external link uses XFN as well? For instance, its REL attribute may be equal to “external coworker” or “external sweetheart met.”
Would the “or equals” method work in such cases, or would the JavaScript need to be changed to look for “external” among any number of possible REL entries?
November 20, 2007 at 3:50 pm
I think the script is just great. Writing XHTML Strict, I still want that for the sake of (many?) visitors some files open in a new window or tab. E.g. pdf and spreadsheets.
November 20, 2007 at 5:14 pm
Excellent. :)
November 26, 2007 at 4:43 pm
That’s a great little piece Perishable - to add my voice to the chorus, there are definitely times when you want a document to open in a new window.
Thanks for the idea!
July 12, 2008 at 4:38 pm
This is great code but I was looking for a wordpress plugin. I figure others will find this who want a wordpress plugin so here’s what I found: http://wordpress.org/extend/plugins/blank-target-replacement/ The code may need to be tweaked to work like comment number five buts it’s easier than starting from scratch. Thanks!
Trackbacks / Pingbacks
Share your thoughts..
← Previous post • Next post →
« How to Fix the Wonky Windows XP Clock • Creating the Ultimate htaccess Anti-Hotlinking Strategy »
1 • Louis
November 20, 2007 at 1:50 pm
“Open External Links as Blank Targets”
It just feel so wrong !
I can’t believe you advice us to use target=_blank links. It’s been highly debated that no one should force the user’s browser to behave differently.
If a user wants to open a link in another tab, then he justs ask for it, by middle-clicking or by another way. It’s a very bad thing to impose him to open a new page (yes, in Safari, target=_blank does not create a new tab, but a new windows, even with tabs on)
Aren’t you upset when a link does not behave as intended ?