javascript - <link> onerror do not work in IE -
i trying attach event handler onerror
event (404 error) <link>
element.
i have on page:
<link rel="stylesheet" type="text/css" href="dead/link.css" onerror="handle404error()" />
it works fine on chrome , opera should work on ie9+ too. found this, can't find solution myself.
is there way without writing method load styles dynamically?
note: didn't tag question 'jquery', please don't use in answers.
in ie, onerror
event not fire on invalid link
urls, onload
event does.
in chrome, opposite true: onload
event not fire on invalid link
urls, onerror
event does.
so you'll need use both events:
<link rel="stylesheet" type="text/css" href="dead/link.css" onload="handle404error(this)" onerror="handle404error(this, true)" /> function handle404error(el, failed) { if(failed || (el.sheet.cssrules && !el.sheet.cssrules.length)) { //failed! } else { //success! } }
example using invalid url:
example using valid url:
http://jsfiddle.net/et0g2xg6/1
update august 2017, @alex:
onerror
fired chrome , firefox.
onload
fired internet explorer.
edge fires neitheronerror
noronload
.
Comments
Post a Comment