Questions tagged «onerror»

15
如何判断<script>标签是否加载失败
我正在向&lt;script&gt;页面的中动态添加标签&lt;head&gt;,并且我想知道加载是否以某种方式失败-404,加载的脚本中的脚本错误等。 在Firefox中,这有效: var script_tag = document.createElement('script'); script_tag.setAttribute('type', 'text/javascript'); script_tag.setAttribute('src', 'http://fail.org/nonexistant.js'); script_tag.onerror = function() { alert("Loading failed!"); } document.getElementsByTagName('head')[0].appendChild(script_tag); 但是,这在IE或Safari中不起作用。 有谁知道在Firefox以外的浏览器中实现此目的的方法吗? (我认为不需要在.js文件中放置特殊代码的解决方案是一个很好的解决方案。这种解决方案既笨拙又不灵活。)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.