PDA

View Full Version : howto: automatically change the href target if href is #



simplifyit
08-13-2006, 04:07 PM
Hi All,

I am looking for a simple javascript that i can had to a page that will automatically search all of the links on a page and if it finds any that are
<a href="#">anylink</a>will add target="_self" these specific links so that they become
<a href="#" target="_self">anylink</a>

Basically all i want to do is add the target reference on for hash links.

Hope that makes sense...

Any and all help very appreciated! :D

Cheers,
Darren

Twey
08-13-2006, 04:11 PM
function addEvent(el, ev, f) {
if(el.addEventListener)
el.addEventListener(ev, f, false);
else if(el.attachEvent)
el.attachEvent("on" + ev, f);
else
el['on' + ev] = f;
}

addEvent(
window,
"load",
function() {
for(var i = 0, e = document.links || document.getElementsByTagName("a"); i < e.length; ++i)
if(e[i].href && e[i].href === "#")
e[i].target = "_self";
}
);