[英]jQuery $(document).ready error blocking footer script
So I've got a unique problem. 所以我有一个独特的问题。
I've got a site that I can't edit the template for at all, I can only alter the embedded script. 我有一个站点,根本无法编辑模板,只能更改嵌入式脚本。
This embedded script is injected into the footer of the site in question. 该嵌入式脚本被注入到相关站点的页脚中。
The embedded script relies on $(document).ready
to kick itself off. 嵌入式脚本依靠
$(document).ready
开始。
The problem is, a script higher up on the page throws an error in it's $(document).ready
function which prevents my $(document).ready
from ever being called. 问题是,页面上方的脚本在
$(document).ready
函数中引发错误,从而阻止了我的$(document).ready
被调用。
I tried setting up a try .. catch
block, like this , but I believe this will only work if my script is higher up on the page still. 我试图像这样设置一个
try .. catch
块,但是我相信只有当我的脚本在页面上还是更高时,这才起作用。
My question, is it possible to get my $(document).ready
call to run, even if it's lower on the page, and a previous one has errors? 我的问题是,是否可以让我的
$(document).ready
调用运行,即使它在页面下方,并且前一个有错误?
In the site above my script: 在我脚本上方的站点中:
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#logo').vAlign();
});
</script>
.vAlign();
is not defined so it's throwing: Uncaught TypeError: undefined is not a function
没有定义,所以抛出:
Uncaught TypeError: undefined is not a function
In my embedded js: 在我的嵌入式js中:
jQuery(document).ready(function() {
console.log('ready!');
});
I never see "ready!" 我从来没有看到“准备好了!” in the console.
在控制台中。
When important operations must always be executed finally-codeblock of try-catch could be used. 当必须始终执行重要的操作时,可以使用try-catch的代码块。 Even if catch-codeblock is not run through finally-codeblock is and so callReady -function is called definitely at the end no matter whether there was an error or not(except for syntax errors).
即使catch代码块没有通过finally代码块运行,所以无论是否有错误(语法错误除外),都一定在最后调用callReady -function 。 As it is the case below:
情况如下:
<script language="JavaScript" src="https://code.jquery.com/jquery-1.11.2.js" type="text/javascript"></script>
<script type="text/javascript">
try{
jQuery(document).ready(function($) {
$('#logo').vAlign();
});
}
catch(e){
console.log(e);
}
finally{
// some fallback code might be executed here
callReady();
}
jQuery(document).ready(callReady);
function callReady(){
console.log('ready!');
}
</script>
Unfortunately if there is no error callReady is called twice(because finally is always run through at the end) but you can check for this case: 不幸的是,如果没有错误,则两次调用callReady (因为最终总是在最后运行),但是您可以检查这种情况:
<script type="text/javascript">
var errorHappened = true;
function checkForErrors(callback){
callback();
// when interpreter reaches this point no error with business-code is thrown
errorHappened = false;
}
try{
jQuery(document).ready(function($) {
try{
checkForErrors(function(){
// put business-code in here
$('#logo').vAlign();
})
}
finally{
if(errorHappened){
callReady();
}
}
});
}
catch(e){
console.log(e);
}
jQuery(document).ready(callReady);
function callReady(){
console.log('ready!');
}
</script>
An error in one script tag in the page doesn't keep other scripts from running. 页面中一个脚本标记中的错误不会阻止其他脚本运行。
Example: 例:
<script>
$(function(){ console.log(1); });
</script>
<script>
$(function(){ console.log 2; });
</script>
<script>
$(function(){ console.log(3); });
</script>
This will first log a syntax error for the second script tag, then it will log the values 1 and 3 as the other ready events work fine. 这将首先记录第二个脚本标记的语法错误,然后将记录值1和3,因为其他就绪事件可以正常工作。
Demo: http://jsfiddle.net/Guffa/am4f7f18/ 演示: http : //jsfiddle.net/Guffa/am4f7f18/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.