The difference between onmousedown and onclick in JS function

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.