Open your external links in a new window automatically

(demo page)

This script initiates on page load and goes through all links (anchor tags) in document. It then checks links, and based on current domain name it recognizes link as internal or external. Furthermore, to external links script adds a class name "external". That way you can visually separate them from internal links.

this.blankwin = function(){
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();
	var a = document.getElementsByTagName("a");	
	this.check = function(obj){
		var href = obj.href.toLowerCase();
		return (href.indexOf("http://")!=-1 && href.indexOf(hostname)==-1) ? true : false;
	};
	this.set = function(obj){
		obj.target = "_blank";
		obj.className = "external";
	};	
	for (var i=0;i<a.length;i++){
		if(check(a[i])) set(a[i]);
	};		
};

back to the article

Blank window script is brought to you by Css Globe