avoiding "javascript:;" in status bar, when used as anchor href

Discussion in 'JavaScript' started by Birmingham, Mar 12, 2008.

  1. #1
    Hi, as far as I'm aware the best practice for setting onclick events with anchors is something like <a href="javascript:;" onclick="..."> ... but this produces an ugly "javascript:;" message in the status bar on most browsers.... so surely there's gotta be a better way?

    is there no better way for accessibility?
     
    Birmingham, Mar 12, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    <a href="#" onclick="foo(); return false;">bar</a>
    
    HTML:
     
    nico_swd, Mar 12, 2008 IP
  3. Birmingham

    Birmingham Peon

    Messages:
    322
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    that defeats the status bar effect, but i heard it's not good for accessibility due to it sending non-javascript users to "#" which is normally top of page. did i mis-hear?
     
    Birmingham, Mar 12, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Yep.

    But you could drop the whole href attribute at all.
     
    nico_swd, Mar 12, 2008 IP
  5. GreatMetro

    GreatMetro Peon

    Messages:
    117
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You could also try:
    <style>
    .spanLink {
    cursor: pointer;
    }
    </style>
    <span class="spanLink" onclick="doSomething();">Link Text</span>
     
    GreatMetro, Mar 13, 2008 IP
  6. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #6
    If we're talking accessibility, how do you activate that with the keyboard?
    It probably would have to be something like:
    <span tabindex=1 class="spanLink" onkeypress="doSomething();" onclick="doSomething();">Link Text</span>
    
    Code (markup):
    The best way to cater for non-JS users is either to provide an alternate URL.
    <a href='alternateURL.htm' onclick="doSomething();return false">Link</a>
    Code (markup):
    or less preferably to have script-generated links.

    Here's another idea that I won't actually code.

    All links with onclick handlers are given href and title attributes thus:
    <a href="#REQUIRES_JAVASCRIPT" title="REQUIRES_JAVASCRIPT  The proper title text" onclick='dosomething();return false'>
    Code (markup):
    Then on page load, loop through all links stripping 'REQUIRES_JAVASCRIPT' from the href and title.
     
    Logic Ali, Mar 13, 2008 IP
  7. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you want the page to be accessible, the href would be to a normal, static page anyway

    
    <a href="page.html" onclick="showpage(); return false;">Fork</a>
    
    Code (markup):
     
    decepti0n, Mar 17, 2008 IP