Home >Web Front-end >JS Tutorial >Discussion on the difference and connection between onmousedown and onclick in js function_Basic knowledge

Discussion on the difference and connection between onmousedown and onclick in js function_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:33:381841browse

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:

Copy code The code is as follows:

function test()
{
(new Image()).src="1.html";
window.open("1.html","_new");
//alert("ok");
return true;
}

I set in the 1.html file, when 1.html does not The jump has been executed before it is fully rendered. The new image function is not guaranteed to have been executed.
When using a single thread to execute a function, you can use the return test() method.

Solution 2: Estimate the maximum execution speed of the multi-threaded function of the test function, add the execution time to 100. Do not use jumps in the a tag. Use setTimeout(location.href="1.html",100) in the test function to perform the jump.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn