On the performance and solution of onchange event under IE and FF _javascript skills

Source: Internet
Author: User

In a recent project, there is a function point: there is a checkbox on the page that sends a JSONP request to the background when the user chooses or cancels the checkbox. The implementation was for this checkbox to add a onchange event, but the result is unexpected, for this reason, I studied in depth, found that the onchange event in IE and FF under the performance of the following problems.

problem ①: under FF, when the checkbox is selected, the onchange event is triggered immediately. However, when changing the checkbox's selected state in IE, it does not start onchange events immediately, but waits for the Checbox to lose focus before the event starts.

To solve this problem, I added the This.blur () statement to the checkbox's OnClick event because the onclick event was executed before the onchange event, adding This.blur in the onclick event () The checkbox loses focus and immediately departs onchange event. In this way, a second problem has been encountered.

problem ②: when the onclick event and This.blur are used at the same time, the error will be in IE.

Looked up some information on the net, finally discovered the Onpropertychange this event. This event is not triggered under FF. In IE, when the checkbox's selection state changes, it will start immediately. Thus, the final solution is obtained: under IE, the checkbox is bound to the Onpropertychange event, and under FF, the onchange event is bound to it.

The specific code implementation is as follows:

Copy Code code as follows:

var ua=navigator.useragent.tolowercase ();
var s=null;
var browser={
MSIE: (S=ua.match (/msie\s*) ([\d\.] +)/))? S[1]:false,
Firefox: (S=ua.match (/firefox\/) ([\d\.] +)/))? S[1]:false,
Chrome: (S=ua.match (/chrome\/) ([\d\.] +)/))? S[1]:false,
Opera: (S=ua.match (/opera). ( [\d\.] +)/))? S[1]:false,
Safari: (S=ua.match (/varsion\/) ([\d\.] +). *safari/)? s[1]:false
};
if (Browser.msie) {//If IE browser
Checkbox.onpropertychange=function () {
Do someting
}
}
else{
Checkbox.onchange=function () {
Do something
}
}

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.