My Page
Teaser text...
Perhaps when you first started learning about jQuery event handling, the first example you saw was about how to prevent the browser from executing the default behavior, such as the following code demonstrating a click event:
Examples like the above will make users develop the bad habit of using "return false" to prevent the browser from executing the default behavior. In this article, I will discuss two very important ways to prevent the browser from executing the default behavior. Important topics:
•Choose the right method: return false or preventDefault, stopPropagation or stopImmediatePropagation
•Choose the appropriate position, start, end, or middle Somewhere: In which part of the event callback should you cancel the browser's default behavior?
Note: When I mention event bubbling in this article, what I want to express is that most events occur initially It is triggered on the DOM, and then goes up through the DOM tree, triggering on each level of parent elements. The event will not bubble up on sibling nodes or child nodes (when the event bubbles down, we call it event capture (event capturing)), you can learn more about event bubbling and capturing here.
Choose the right method
The reason why "return false" is so misused is because it looks like it has completed the job we gave it, the browser will no longer redirect us to the link in the href, and the form will no longer redirect us to the link in the href. Will continue to be submitted, but what's wrong with doing this?
What exactly does "return false" do?
Every time you call "return false", it actually does 3 things:
•event.preventDefault();
•event.stopPropagation();
•Stop callback function execution and return immediately.
"Wait a minute", you yelled! I just want the browser to stop executing the default behavior, I don't need it to do the other 2 things.
Of these 3 things, only preventDefault is used to prevent the browser from continuing to perform the default behavior. Unless you want to stop event bubbling, using return false will plant a lot of hidden dangers for your code. Let's go through a Let’s see the consequences of such misuse with a real example:
This is the HTML we used to demonstrate:
If we mix it with live or delegate events, the situation will be worse.
preventDefault()
Most of the time, when you use return false, what you really need is e.preventDefault(). To use e.preventDefault, you need to make sure you pass the event argument to your callback function (in this case, the e):
stopPropagation()
But there are situations where you may need to stop event bubbling, let’s look at the following example:
This method will stop an event from continuing to execute. Even if other processing functions are bound to the current object, all events bound to an object will be executed in the binding order. Take a look at the following example:
Copy code
return false
Only if you need preventDefault and stopPropagation at the same time, and your code can accept the browser's default behavior of not stopping until your callback execution is completed, then you can use "return false". But I strongly recommend that you don't use this method in demo code written for other jQuery developers, because it will lead to more misuse, and only use "return false" when you are sure that it is absolutely necessary.
Choose the appropriate location
If you use "return false", it will only cancel the browser's default behavior after your callback function is executed, but with e.preventDefault, we have more options, it can stop the browser at any time Performs the default action regardless of where in the function you place it.
1. During the development phase, you should always put it on the first line. The last thing you want to do may be that when you are debugging to change a form to ajax submission, it has already been submitted according to the old method.
2. Product stage, If you use progressive enhancement, put it at the end of the callback, or at the logical end point. If you use progressive enhancement on a normal page, Then you need to consider on the server side how to handle the link's click event and the form's submit event if the browser does not support JS (or is disabled). The advantage here is that we do not consider the case of turning off JS, only the madness when supporting js. If your callback code makes an error and throws an exception, let us look at the following code:
3. In the product stage, If the function is designed in JS, it should be placed in the first line.
Remember, it doesn’t have to be the first line of the function, but the earlier the better. The principle here is: if the function of the function is implemented through JS (does not involve server-side interaction), then there is no need to consider compatibility. In this case, adding it on the first line will prevent the # character from appearing in the URL, but obviously you should still add as much error handling code as possible to prevent the user from becoming confused when an error occurs.
Conclusion
I hope this article conveys enough information for you to make the right choice if you need to prevent your browser from performing default behavior. Remember, only use "return false" if you really know what you are doing, and make sure you are calling the corresponding code at the correct place in the function. Finally, keep your code as flexible as possible and try not to use "return false" again!