Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: get the href value

  1. #1
    Join Date
    Oct 2006
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default get the href value

    Hello,

    I am trying to implement something like this but no lack in my code:

    Code:
                var deadlinks=document.getElementsByTagName("a");
    		for (var i=0;i<deadlinks.length;i++){
    			if(deadlinks[i].href == '#')	{		
    			deadlinks[i].onclick = openModal;		
    		}
    	    }
    What i'm trying to achieve is get all dead links with an href="#" value and assign to them a function.

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    That should work, but a another way of assigning functions to anchors is using javascript:void(statement[s]):
    Code:
    <a href="javascript:void(alert('clicked'); var x=9; alert(x*3);)">Click Me</a>
    Unless you are using the this keyword in your onclick, I think this is a better way of doing things.

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It's not. It's a very poor way of doing things. It's an abuse of both the href attribute and the javascript: pseudo-URL scheme. Links with href attributes are meant to direct the browser somewhere, and at least IE assumes this and begins closing down the page ready to load a new page, by stopping animated images for example.

    Furthermore, if you have links calling Javascript to which you can apply no more appropriate fallback than "#", then the chances are they shouldn't be links at all.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    The main reason why I don't like "#" is because it messes up with the back and forward buttons, but then again, I didn't know about that IE thing.

    So I guess the best thing to do could be using css to make a span class behave like a link visually and give it onclick attributes.
    Last edited by Trinithis; 06-08-2007 at 07:11 PM.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    A button would be more semantically appropriate, actually. The <span> is better than using a link, though, certainly.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    But in some cases you have to use a link. Like if need to use the tab index.

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Buttons can have a tabindex too.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You can mix javascript:void() and onclick, or even make custom 'javascript:' links that are informative while doing nothing else. This may not be the best way in certain respects, but causes no problems in the majority of browsers (any that I know of), and can be, as I said, informative. For example, if you have links that change images:

    HTML Code:
    <a href="javascript:next()" onclick="changeImage(1);return false;">Next</a>
    <a href="javascript:previous()" onclick="changeImage(-1);return false;">Previous</a>
    These 'links' will never fire, but will show up in the status bar and/or other places in many browsers, giving the user information about what will happen if they click. The functions, next() and previous() don't even need to exist.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Oct 2006
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well the reason i want to achieve this is because i don't want to manually add the function in every anchor tag with href="#".
    If i have 15 anchor tags with href="#" in evety page, i want to assign to all of them an action.

    But for some reason this
    Code:
    if(deadlinks[i].href == '#')
    is not working. Is it some way that i can get what is the value of the href??

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The browser will convert the href to an absolute form, so you really want to check:
    Code:
    if(deadlinks[i].href.lastIndexOf("#") === deadlinks[i].href.length - 1)
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •