This article mainly introduces the performance and solution of the onchange event in IE and FF. If you need it, you can refer to it, I hope it will be helpful to you. In a recently developed project, there is a checkbox on the page. When you select or cancel this checkbox, A jsonp request will be sent to the backend. At that time, the implementation was to add an onchange event for this checkbox, but the results were unexpected. For this reason, I have studied it in depth, the following problems exist in the performance of the onchange event in IE and FF.
Question ①:In FF, The onchange event is triggered immediately when the checkbox is selected. However, when I change the checkbox selected status in IE, The onchange event will not be started immediately. Instead, the event will start only when the checbox loses focus.
To solve this problem, I added this in The onclick event of the checkbox. blur (), because the onclick event is executed before the onchange event, so add this in The Onclick event. blur () causes the checkbox to lose focus and the onchange event will be triggered immediately. However, the second problem is encountered.
Question ②:When the onclick event and this. blur are both used, an error is reported in IE.
I found some information on the Internet and finally found the onpropertychange event. This event will not be triggered in FF. In IE, when the checkbox selection status changes, it will start immediately. Therefore, the final solution is obtained: in IE, bind the onpropertychange event to the checkbox, and in FF, bind the onchange event to it.
The Code is as follows:
The Code is 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) {// for IE browser
Checkbox. onpropertychange = function (){
// Do someting
}
}
Else {
Checkbox. onchange = function (){
// Do something
}
}