SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Best way to deal with null links

    Hi guys,

    A problem popped up this morning that I have always known about but never had to deal with until now. What if I need to display an anchor link which goes nowhere, I have two choices right?

    a href="javascript:void(0)"

    or

    a href="#"

    I am currently building a chromeless YouTube player and the links I want to be nullified are the buttons that are calling its api functions. So I need the correct cursor behaviour but not the anchor behaviour. The # option is definitely the wrong one because it refreshes the current page but on the other hand I haven't seen javascript:void(0) used for years. Is it better for me to just use CSS to change the cursor behaviour and not use <a> tags at all?

    I appreciate any advice
    "Persistence is the path to perfection"

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,120
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    I think there are lots of debates over this. An alternative you could try is

    Code:
    <p><a href="#me" id="me" onclick="return false;">Something</a></p>
    The ideal would be to have the JS out of the HTML altogether, but I'm not quite sure how to write that. (It's a job for the JS forum. )

    PS: the real problem here is having a link that does nothing with JS off. Ideally, users without JS shouldn't be left with a dead link. If that's the case, it suggests there's a better solution.
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  3. #3
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Ralph that's interesting, so basically you are targeting the anchor to itself so that you don't get the page scrolling.
    I guess you could just use jquery :

    $("#me").click(
    function{
    do something()
    });

    That way you are targeting the link with the id of me without having to have any inline scripting.
    Thanks again
    "Persistence is the path to perfection"

  4. #4
    Pedantic Semantic blain's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    First off, if the anchor only exists to bind an ajax event to, then the anchor should be put there via javascript.

    Then you should do something like
    Code:
    $('.null').click(function(e){
       //do something()
       e.preventDefault();
    });
    Technology is dominated by two types of people:
    those who understand what they do not manage,
    and those who manage what they do not understand.


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
  •