These two events are very common.
Understand the difference between the two events, but actually do not use the difference between the two events to do some action.
Usually use the onclick time also can use onmousedown, use the onclick time more.
Today, I came across a time when I was not using onmousedown, so I hereby record.
First of all, the difference: OnClick is triggered after the mouse clicks bounce event. OnMouseDown are events that are triggered after the mouse is pressed.
Simple to say onclick = onmousedown + onmouseup;
If you press the mouse and move the mouse over one place, the onmousedown event is triggered by releasing the mouse in another place, but the onclick event is not triggered.
The function we are going to implement today is to click a tab, switch to another page, and trigger a function before the original page closes. Not windowunload.
Previously used is the onclick event, ie Test no problem, but in FF test found that the page was destroyed before the execution of this function.
If you use the OnMouseDown event, there is no way to guarantee that the function must be executed, but it has been fought for a certain amount of time because the jump was performed after the mouse bounced.
Workaround one: onclick= "return Test ()" To perform a jump if and only if the function returns True. This leads to a new problem. If we execute a multithreaded program or a new thread in the test function, we first return true, jump, and the newly opened threads are not executed. As the following function:
The code is as follows:
function test ()
{
(New Image ()). src= "1.html";
window.open ("1.html", "_new");
Alert ("OK");
return true;
}
I set it in a 1.html file and the jump has been executed when 1.html is not fully rendered. The new image function is also not guaranteed to have been executed.
You can use the return test () method when you are using a single thread to execute a function.
Workaround Two: Estimate the maximum execution speed of the multithreaded function of the test function, and add execution time to 100. Do not use jumps in a label. Use SetTimeout (location.href= "1.html", 100) in the test function to perform a jump.