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!
AI-powered app for creating realistic nude photos
Online AI tool for removing clothes from photos.
Undress images for free
AI clothes remover
Swap faces in any video effortlessly with our completely free AI face swap tool!
Easy-to-use and free code editor
Chinese version, very easy to use
Powerful PHP integrated development environment
Visual web development tools
God-level code editing software (SublimeText3)
The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.
Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in
Vue3.2 setup syntax sugar is a compile-time syntax sugar that uses the combined API in a single file component (SFC) to solve the cumbersome setup in Vue3.0. The declared variables, functions, and content introduced by import are exposed through return, so that they can be used in Vue3.0. Problems in use 1. There is no need to return declared variables, functions and content introduced by import during use. You can use syntactic sugar //import the content introduced import{getToday}from'./utils'//variable constmsg='Hello !'//function func
Usage of return in JavaScript requires specific code examples In JavaScript, the return statement is used to specify the value returned from a function. Not only can it be used to end the execution of a function, it can also return a value to the place where the function was called. The return statement has the following common uses: Return a value The return statement can be used to return a value to the place where the function is called. Here is a simple example: functionadd(a,b){
JavaScript functions provide two interfaces to interact with the outside world. The parameters serve as the entrance to receive external information; the return value serves as the outlet to feed back the operation results to the outside world. The following article will take you to understand the JavaScript function return value and briefly analyze the usage of the return statement. I hope it will be helpful to you!
How to use return in JavaScript requires specific code examples. In JavaScript, return is a very important keyword. It is usually used to return a value in a function or end the execution of a function. The return statement is used to return a value to the caller of the function and terminate the execution of the function. The return statement can be used anywhere in a function and can return any JavaScript data type, including numbers, strings, booleans,
Python return value return usage is that when the function executes the return statement, execution will stop immediately and the specified value will be returned to the place where the function was called. Detailed usage: 1. Return a single value; 2. Return multiple values; 3. Return a null value; 4. End the execution of the function early.
The project recently added an IP black and white list function. I found that if the IP filtering interceptor returns false, the front end will display cross-domain. After trying to modify the MVC configuration class, I found that it still didn't work. Finally, I added a judgment to the interceptor @OverridepublicbooleanpreHandle. (HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsIOException{//-----------Just add this-----------if(!(handlerinstanceo