I have a requirement that the dynamically created script tag should be removed in any case. There are four available options: onload, ontimeout, and onerror. Therefore, we need to check whether the browser supports onerror events. Confirmed by the naked eye, IE6 ~ Both opera11 and 8 are not supported. Safari, chrome, Firefox, and ie9 are supported. However, opera cannot be programmed to detect support for opera (with high finger points in my power ).
</P> <p> var El = document. createelement ("script"); <br/> document. body. insertbefore (El, null); <br/> var name = "onerror" <br/> alert (name in El); <br/> el. setattribute (name, ""); <br/> alert (typeof El [name] = "function"); <br/> el. onerror = function () {<br/> alert ("onerror events supported") <br/>}< br/> el. src = "test _" + 1 * new date; <br/> setTimeout (function () {<br/> var parent = el. parentnode <br/> If (parent & parent. nodetype = 1) {<br/> If (El. clearattributes) {<br/> el. clearattributes (); <br/>}else {<br/> el. onerror = NULL; <br/>}< br/> parent. removechild (EL) <br/> El = Name = NULL; <br/>}< br/>}, 2000) </P> <p>
RunCode
Seeing that ie678 and opera are just a living treasure, I really want to cut the opera guys!
Now we can only use this code to differentiate:
// By situ zhengmei var suppscripscripterror = (! Window. Opera) &-[1,];
Related link: checks the browser's support for events