Why in the video tutorial from mail.ru it is written $('a').click(function() {alert('lalala'); return false;}); and when you click on the link, an alert pops up and, after clicking on it, it does not skip the user; when you click on the link again, an alert pops up.

And in my code, <div class="header__list_raw" onclick="userIsntLogged ();"></div>

js

 function userIsntLogged () { alert('Для начала войдите на сайт'); return false; } 

When you click on the link, it opens the alert after clicking on it, it goes to the link, and when you click on the link again, the alert link does not pop up until you refresh the page.

  • Because onclick="return userIsntLogged()" . But do not need to write like that. - Alexey Ten
  • And I didn’t understand what kind of "link" I’d click on the div ... - MedvedevDev

1 answer 1

The problem is that you set up an event handler in different ways.

 $('a').click(function() {alert('lalala'); return false;}); <div class="header__list_raw" onclick="userIsntLogged ();"></div> 

In the first case, an event handler is hung on a click on the link and false returned inside to stop the ascent and the default behavior. When you click on it, a function is executed that also returns false , but there is one BUT

Your js code does something like this.

 <div onclick="function(event) { userIsntLogged (); }"> </div> 

That is, in fact, js wraps your code in a function. What would work, what exactly this "native" function would need to return false

 <div onclick="userIsntLogged (); return false"> </div> // либо <div onclick="return userIsntLogged ();"> </div> 

PS
Returning false no longer fashionable, this is too implicit a method, because besides canceling the default action, it also stops the ascent. Use better stopPropogation , preventDefault

 <div onclick="userIsntLogged (event);"> </div> function userIsntLogged (e) { e.stopPropagation(); // отменяем всплытие e.preventDefault(); // отменяем действие по умолчанию alert('Для начала войдите на сайт'); } 
  • Now it works, thank you - Andrey Golubev