PDA

View Full Version : JS Newbie - Using Nodes to this.blur() all links


Snetty
10-10-2006, 03:05 AM
Ok, firstly I'm totally new to JS but I've been reading up a bit on various things and I think that it's possible to use nodes to set onclick='if(this.blur)this.blur()' for all links without actually entering it in the html every singe time (which tbh, I keep forgetting to do).

I've written the following code.. which surprise surprise doesn't work (but also doesn't give me any errors)

<script language='Javascript' type='text/javascript'>

startList = function() {

for (i=0; i<document.body.childNodes.length; i++) {

node = document.body.childNodes[i];

if (node.nodeName=="a") {

node.onfocus="if(this.blur)this.blur();";

}

}

}

window.onload=startList;

</script>

Does anyone think that they can point me in the right direction?

felgall
10-10-2006, 04:03 AM
The following would be a more efficient way to access all the links on a web page:

startList = function() {
for (i=0; i<document.links.length; i++)
document.links[i].onfocus="if(this.blur)this.blur();";
}

Snetty
10-10-2006, 11:45 AM
thanks for the help felgall, but unfortunately that doesn't seem to be working for me. Same situation, no error messages, it just doesn't work. I'm using FF if that makes any difference.

Kor
10-10-2006, 02:11 PM
use an anonymous:

document.links[i].onfocus=function (){if(this.blur){this.blur()}}

But I am not very sure about IE if supports focus/blur on links...

On the other hand, just an informal note: In your previous code you should have used uppercase when searching for nodeName

if (node.nodeName=="A")

or

if (node.nodeName.toLowerCase()=="a")


Another informative note could be that you may also use the tagName:


document.getElementsByTagName('a')

Snetty
10-10-2006, 04:25 PM
many thanks, i'll give that a go...

p.s. mufc ftw!

Snetty
10-10-2006, 04:48 PM
that worked beautifully.. all sorted. many many thanks

Bill Posters
10-10-2006, 05:10 PM
that worked beautifully.. all sorted. many many thanks

Techniques such as this should be used with caution, if not avoided altogether as they cripple navigation for keyboard users with js enabled.

You should consider a less destructive approach…

e.g.
http://www.cssplay.co.uk/menu/nodots.html