Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • return false vs event.preventdefault

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 126
    Comment on it

    Return false will perform three task when called

    1 event.preventDefault();

    2 event.stopPropagation();

    3 Stops callback execution and returns immediately when called.

    The preventDefault() method cancels the event if it is cancellable, meaning it prevents the default action that belongs to the event to occur. The benefit to using event.preventDefault() is that you can add this as the first line in the handler, thereby guaranteeing that the anchor's default behavior will not fire, regardless if the last line of the function is not reached (eg. runtime error) which is not the case .lets assume of return false If your click handler uses return false to prevent browser navigation, it opens the possibility that the interpreter will not reach the return statement and the browser will proceed to execute the anchor tag's default behavior. return false example.

    $('a').click(function (e) {
      // custom handling here
    
      // oops...runtime error..
    
      return false;
    });
    

    preventdefault() example.

    $('a').click(function (e) {
      e.preventDefault();
    
      // custom handling here
    
      // .runtime error, but  the user isn't navigated away.
    });
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: