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.
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
  1. #16
    New Coder
    Join Date
    Dec 2010
    Posts
    99
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Sovled!

    Thank you so mutch for your patience and your pedagogical teaching. I learnd so much today and are so happy for that!

    The code works perfect!


  2. #17
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    487
    Thanks
    0
    Thanked 54 Times in 53 Posts
    @Kor: With all due respect (and I'm not doing this to be argumentative, please don't take it as such), that is not correct.

    Yes, these two versions indeed act differently because they are coded differently as inline event handlers.
    Code:
    <a href="http:www.google.com" onclick="preventLinkAction()">click</a>
    
    <a href="http:www.google.com" onclick="return preventLinkAction()">click</a>
    The function called in the first version may as well not return anything at all since that return value is never used.

    Quote Originally Posted by Kor View Post
    No. You must ask also for a return within the handler.
    Exactly. When the handler is attached from within a script (not inline as above), the return from within the handler executes whether it is assigned by reference or executed within an anonymous wrapper.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Prevent Link</title>
    <script type="text/javascript">
    
    function preventLinkAction(){
      return confirm("Follow link?");
    }
    window.onload = function(){
      document.getElementById("linkA").onclick = function(){return preventLinkAction();};
    
      document.getElementById("linkB").onclick = preventLinkAction;
    }
    </script>
    </head>
    <body>
    <a href="http://google.com/" id="linkA">link A</a>
    <a href="http://google.com/" id="linkB">link B</a>
    </body>
    </html>
    The two links in the demo above behave identically.

    Since there is no problem interpreting the return from the handler, is there a specific reason to wrap the handler in an anonymous function? Again, I'm not asking to be argumentative, I'm wondering if there is an advanced issue (closure, etc.) that I'm missing here.

  3. #18
    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
    Quote Originally Posted by kansel View Post
    Again, I'm not asking to be argumentative, I'm wondering if there is an advanced issue (closure, etc.) that I'm missing here.
    Agree. It looks like we were barking at the wrong tree
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


 
Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

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