Web Analytics Made Easy -
StatCounter JS Newbie - Using Nodes to this.blur() all links - CodingForum

Announcement

Collapse
No announcement yet.

JS Newbie - Using Nodes to this.blur() all links

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • JS Newbie - Using Nodes to this.blur() all links

    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)

    Code:
    <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?

  • #2
    The following would be a more efficient way to access all the links on a web page:

    Code:
    startList = function() {
    for (i=0; i<document.links.length; i++) 
          document.links[i].onfocus="if(this.blur)this.blur();";
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

    Comment


    • #3
      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.

      Comment


      • #4
        use an anonymous:
        Code:
        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
        Code:
        if (node.nodeName=="A")
        or
        Code:
        if (node.nodeName.toLowerCase()=="a")
        Another informative note could be that you may also use the tagName:

        Code:
        document.getElementsByTagName('a')
        Last edited by Kor; Oct 10, 2006, 03:39 PM.
        KOR
        Offshore programming
        -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

        Comment


        • #5
          many thanks, i'll give that a go...

          p.s. mufc ftw!

          Comment


          • #6
            that worked beautifully.. all sorted. many many thanks

            Comment


            • #7
              Originally posted by Snetty View Post
              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
              Why you should validate your code before asking for help...

              Comment

              Working...
              X