Differences and connection between onmousedown and onclick in js Functions

Source: Internet
Author: User

These two events are common.
I understand the differences between the two events, but I didn't actually use the differences between the two events to perform some operations.
Generally, onmousedown can be used when onclick is used, and more can be used when onclick is used.
We hereby record the problem that onmousedown cannot be used today.

Differences: Onclick is an event triggered after the mouse clicks. Onmousedown is an event triggered after the mouse is pressed.
In short, onclick = onmousedown + onmouseup;
If you move the mouse away from another place, releasing the mouse will trigger the onmousedown event, but the onclick event will not be triggered.
The function we want to implement today is to click the tag and switch to another page. a function needs to be triggered before the previous page is closed. Not windowunload.
Previously, the onclick event was used, and the test in IE was normal. However, the test in FF found that the function had not been executed before the page was destroyed.
If the onmousedown event is used, although there is no way to ensure that the function will be executed, it takes some time to execute the function, because the jump is executed after the mouse pops up.

Solution 1: Onclick = "return test ()". The jump is executed only when the function returns true. This leads to a new problem. If we execute a multi-threaded program or a new program in the test function, true is returned first, and the new thread is not executed. The following functions:Copy codeThe Code is as follows: function test ()
{
(New Image (). src = "1.html ";
Window. open ("1.html", "_ new ");
// Alert ("OK ");
Return true;
}

In the 1.html file, I set <body onload = "javascript: alert ('OK')">. When 1.html is not completely rendered, the redirection has been executed. The new image function is not guaranteed to be executed.
Return test () can be used to execute functions in a single thread.

Solution 2: It is estimated that the maximum execution speed of the multi-threaded function of the test function is 100. Skip is not used in tag. Use setTimeout (location. href = "1.html", 100) in the test function to perform the 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.