Home >Web Front-end >JS Tutorial >Discussion on the difference and connection between onmousedown and onclick in js function_Basic knowledge
Both events are common.
Understand the difference between these two events, but actually do not use the difference between these two events to do some operations.
Usually you can also use onmousedown when using onclick, more often when using onclick.
Today I encountered a time when I had to use onmousedown, so I will record it here.
Let me talk about the difference first : onclick is an event triggered after the mouse click bounces. onmousedown is an event triggered after the mouse is pressed.
Simply say onclick = onmousedown onmouseup;
If you press the mouse in one place and then move the mouse and release the mouse in another place, the onmousedown event will be triggered, but the onclick event will not be triggered.
The function we want to implement today is to click on the a tag to switch to another page. A function needs to be triggered before the original page is closed. Not windowunload.
The onclick event was used before, and there was no problem when tested in IE. However, when tested in FF, it was found that this function had not had time to execute before the page was destroyed.
If you use the onmousedown event, although there is no way to guarantee that the function will be executed, it will buy a certain amount of time for the execution of the function, because the jump is executed after the mouse bounces up.
Solution 1: onclick="return test()", the jump will be executed if and only if the function returns true. This leads to a new problem. If we execute a multi-threaded program or a newly opened thread program in the test function, it will return true first and jump, and the newly opened thread will not be executed. For example, the following function: