1
<a class='google' href="www.google.com>google</a>

says I can't change the above markup but I want to disable the hyperlink, I do this

$('.google').click(function(){
    return false;
    windows.open('www.gmail.com');
});

I expect after return false my code will work but it doesn't, why?

Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
user3522457
  • 2,845
  • 6
  • 23
  • 24

1 Answers1

10

Because after return statement, rest of the code will never be invoked. That is the default behavior of every programming language. You can prevent the redirection by using event.preventDefault(). For that, use like this

$('.google').click(function (e) {
    e.preventDefault();
    window.open('www.gmail.com');
});

For event.preventDefault() vs return false see event-preventdefault-vs-return-false

Community
  • 1
  • 1
Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53