Wednesday, March 17, 2010

Force links in certain div to open in new window (javascript)

revision of earlier code...useful for Blogger templates.  changes only links in blog post. Add an HTML widget to your template. Add this function in your body onload event<body onload="ForceLinks();"> 

<script language='javascript'>
function ForceLinks()
{
// It will tell u how many no of tags your code have
var linkslength= document.getElementById('Blog1').getElementsByTagName('a').length;

// Putting tag in links variable to use it for array purpose
var links=document.getElementById('Blog1').getElementsByTagName('a');

for(var i = 0; i < linkslength; ++i)

{

links[i].setAttribute('target','_blank');

}

}
</script>

Force all links on page to open in new window (javascript)


function ForceLinks()

{
// It will tell u how many no of tags your code have
var linkslength= document.getElementsByTagName('a').length;

// Putting tag in links variable to use it for array purpose
var links=document.getElementsByTagName('a');

for(var i = 0; i < linkslength; ++i)

{

links[i].setAttribute('target','_blank');

}

}

Now just call this function in your body onload event<body önload="ForceLinks();">