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

Source: Internet
Author: User

  This article is mainly about the onchange event in IE and FF performance and solutions are introduced, the need for friends can come to the reference, I hope to help you.

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.   ①: 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 and therefore added This.blur in the onclick event () causes the checkbox to lose 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 Internet and finally found the Onpropertychange 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.   Specific code implementation is as follows:     code as follows: Var ua=navigator.useragent.tolowercase (); var s=null; var browser={    MSIE: (S=ua.match/msies* ([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, &nbsP   Safari: (S=ua.match (/varsion/) ([D.] +). S[1]:false  };  if (Browser.msie) {//For IE browser     checkbox.onpropertychange=function *safari/)? () {         //do someting    } else{    checkbox.onchange=function () {&N Bsp      //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.