Javascript检测静态< script>加载失败 [英] Javascript detect static <script> load failues
问题描述
这类似于如何判断一个<脚本>标记未能加载。但我的问题特别是关于我的HTML主体中的普通旧< script>
标签,所以
< script type =text / javascriptsrc =/ path / to / my / script.js>< / script>
我有一个自定义 window.onerror
函数我写的处理一般脚本错误。事实上,如果这样的脚本在评估脚本本身时出现语法错误或发生其他运行时错误,那么我的 onerror
函数正确处理该脚本。
但是,如果脚本根本无法加载,因为连接中断或404或其他服务器错误, window.onerror
不叫。如何在JavaScript中以编程方式检测这种失败?
澄清:我意识到我可以将 onerror
属性在每个脚本标记中。我询问是否有更好的方法,可以与我的全局 window.onerror
相比,或者是调用该函数的某种方式。
<您可以在您的脚本标记中检查youruniquefunction
if( typeof youruniquefunction!=='undefined'){
//您的脚本ok
}
This is similar to How to tell if a <script> tag failed to load. But my question is specifically about plain old <script>
tags in my HTML body like so
<script type="text/javascript" src="/path/to/my/script.js"></script>
I have a custom window.onerror
function that I wrote to deal with general script errors. And indeed, if such a script has a syntax error or some other runtime error occurs while evaluating the script itself, that is handled correctly by my onerror
function.
But if the script fails to load at all, because of an interrupted connection or even a 404 or other server error, window.onerror
is not called. How can I detect this kind of failure programmatically in Javascript?
To clarify: I realize I could just put an onerror
attribute in every single script tag. I'm asking if there's a better way, something comparable to my global window.onerror
or some way of making that function be called.
You can check youruniquefunction in your script tag
if (typeof youruniquefunction!== 'undefined' ) {
// your script ok
}
这篇关于Javascript检测静态< script>加载失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!