Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Sep 2005
    Location
    Latina (Italy)
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Not to use <a href="javascript:..."

    some of the links I built were made so.
    I read on this forum it's not a good practice and it's true, so I'm converting them all to the
    Code:
    <a href="..." onclick="foo()">...</a>
    style.
    The problem is that even if I made the onclick-functions return
    Code:
    false
    , the anchor redirects me anyway to the page that's in the href attribute. Is there a reason?
    May someone help me?
    Thanx

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    If you made the function your calling return false, then in your onclick handler call it like so:
    <a href="" onclick="return foo();">link</a>


    Good luck

  • #3
    New Coder
    Join Date
    Feb 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you could use href="#"
    Asp.Net 1.1 - 20 hrs
    CSS 2.0 - 3 hrs
    XHTML 1.1 - 5hrs
    JavaScript - 5hrs
    Cross-Browser Compatibility - Priceless

  • #4
    New Coder
    Join Date
    Sep 2005
    Location
    Latina (Italy)
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For a strange reason, if I set it so:
    <a href="..." onclick="foo();return false;">
    the browser seem to work the right way...

    Maybe once I'll understand
    who knows...!!!
    Thanx anyway!!

  • #5
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Can you explain, i'm not understanding? Are you putting return false in foo()? Post your code example and we'll be able to take a look at it.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,609
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Method 1:

    Code:
    function foo() {
    ....
    return false;
    }
    
    <a href="#" onclick="return foo()">
    Method 2:

    Code:
    function foo() {
    ....
    }
    
    <a href="#" onclick="foo();return false">
    Method 1 allows the function to return true if you want the href to be able to run (in which case you'd replace the # with a new page name).
    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.

  • #7
    New Coder
    Join Date
    Sep 2005
    Location
    Latina (Italy)
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanx Stephen, now I understand!!
    If I don't set a 'return' statement before the function-call, it's not expected the function returns anything!! Here's why!!!!
    Thanx a lot!!!!

  • #8
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by Brandoe85
    If you made the function your calling return false, then in your onclick handler call it like so:
    <a href="" onclick="return foo();">link</a>
    Exactly as I allready stated....

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    The reson for, in that case, you must use return false is only to prevent the page's scrolling up. "#" location will bring the page up, so that you need a return false to prevent this HTML action.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #10
    New Coder
    Join Date
    Sep 2005
    Location
    Latina (Italy)
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you guys.
    sorry Brando I didn't recognize the return statement immediately in your posted code thanx very much to you all!


  •  

    Posting Permissions

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