Not using href when an onlick is specified?

Discussion in 'JavaScript' started by blueparukia, Mar 7, 2008.

  1. #1
    So I have a link:

    
    <a href="index.php"  onclick="js_function()">Link Text</a>
    
    Code (markup):
    What I want to do is when a user clicks the link, the JS function is run, but the browser does not navigate to index.php.

    However, if a user has Javascript disabled and clicks on the link, I'd like the browser to go to index.php.

    Is there any way - in Javascript or PHP - to do this.

    Thanks,

    BP
     
    blueparukia, Mar 7, 2008 IP
  2. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    1) Change the link code to:
    
    <a href="index.php" onclick="return js_function();">Link Text</a>
    
    Code (markup):
    2) Add a return false; statement before the Javascript function terminates:
    
    function js_function ()
    {
        // Do something...
    
        return false;
    }
    
    Code (markup):
     
    vpguy, Mar 7, 2008 IP
  3. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #3
    Sounds good. I'll test it later.

    Thanks,

    BP
     
    blueparukia, Mar 7, 2008 IP
  4. Morishani

    Morishani Peon

    Messages:
    239
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I Think that he wants that the href will work.

    blueparukia if you want the Href to work, do return true, if you don't want it to work do return false .

    Good luck :)
     
    Morishani, Mar 8, 2008 IP
  5. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #5
    Yes I have tried it, and return false does what I want - the href won't work. However if I disable Javscript, it works.

    Thanks all.
     
    blueparukia, Mar 8, 2008 IP
  6. Submerged

    Submerged Active Member

    Messages:
    132
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    Would this work when calling a function on a parent frame? My code is still following the link.

    //topframe.php
    
    echo "<a href=index.php onClick=\"return parent.myPopup($viewedid);\">Click here!</a>";
    
    //frameset.php
    
    <html><head><script>
    function myPopup(id)
    {
    window.open("checkusername.php$id=" + id, "popUp", "status = 1, height = 100, width = 200, resizable = 0" )
    popUp.focus(); 
    return false;
    }
    </script></head>
    Code (markup):
    The popup window shows up fine, but the topframe still follows the link to index.html. I only put up the function related code, there's a good one or two hundred lines on each page that shouldn't really matter. The frameset code is working fine though.
     
    Submerged, Mar 8, 2008 IP
  7. roy.laurie

    roy.laurie Peon

    Messages:
    51
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    With mootools ...

    <a id="foo" [...]
    HTML:
    
    <script type="text/ecmascript"><![CDATA[/*
    $('foo').addEvent('click', function(e)
    {
       new Event(e).stop();
       parent.myPopup(bar);
    
       return;
    });
    */]]></script>
    
    Code (markup):
    Since you're passing an ID to myPopup, I'd recommend giving your anchor tag an id of "foo_$viewid". Then, in the click event handler above, instead of bar, use:
    
    parent.myPopup(this.id.match(/_(\d)+$/)[1]);
    
    Code (markup):
    I use that style all the time. It's very slick.
     
    roy.laurie, Mar 9, 2008 IP