HackToHell. Powered by Blogger.

How to make all external links open in a new tab in blogger

I was browsing some JavaScript and came across a interesting javascript that makes all the external links in a page open in a new tab



This script was written by http://grakalic.com/.

Here it is .


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]);
};
};



// script initiates on page load.

this.addEvent = function(obj,type,fn){
if(obj.attachEvent){
obj['e'+type+fn] = fn;
obj[type+fn] = function(){obj['e'+type+fn](window.event );}
obj.attachEvent('on'+type, obj[type+fn]);
} else {
obj.addEventListener(type,fn,false);
};
};
addEvent(window,"load",blankwin);

So now how do you add it to blogger.

Go to your blogger dashboard.

There in the design tab , Click edit HTML.

Now using search (Ctrl+S) find this <head>

Immediately after it paste the following code.


<meta content='This script automatically sets external links to open in a new window visit http://hacktohell.blogspot.com to get it!' name='description'>
<script src='http://dl.dropbox.com/u/12158677/link.js' type='text/javascript'/>
</meta>


Save the template and you are done.

P.S:-DOWNLOAD YOUR TEMPLATE BEFORE DOING THIS!
Share on Google Plus

About hacktohell

Love technology.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment