忽略错误并继续在 IE 中运行 javascript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9397721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:32:05  来源:igfitidea点击:

Ignore errors and continue running javascript in IE?

javascriptjqueryinternet-explorer

提问by Rob

I've got a JIT Spacetree on my webpage, and IE doesn't like a few lines. If I open the developer tools, and tell it to run through them, it looks great and loads everything as it should.

我的网页上有一个 JIT Spacetree,而 IE 不喜欢几行。如果我打开开发人员工具,并告诉它运行它们,它看起来很棒,并且可以按预期加载所有内容。

Is there any way I can get it to just say "You know what, these errors aren't really deal breakers, let's keep on going here"? The two further indented lines are the offenders, as well as something in jQuery 1.6.4 (will be trying 1.7.1) with either $.getJSON or $.parseJSON

有什么办法可以让它只是说“你知道吗,这些错误并不是真正的交易破坏者,让我们继续在这里”?两个进一步缩进的行是违规者,以及 jQuery 1.6.4(将尝试 1.7.1)中的一些东西,带有 $.getJSON 或 $.parseJSON

    var style = label.style;
        style.width = node.data.offsetWidth;
        style.height = node.data.offsetHeight;            
    style.cursor = 'pointer';
    style.color = '#fff';
    style.fontSize = '0.8em';
    style.textAlign= 'center';
},

回答by DaveS

wrap the offending code in a try/catch, and don't do anything in the catch.

将违规代码包装在 try/catch 中,并且不要在 catch 中执行任何操作。

回答by p1100i

IE is "allergic" in defining an object and leave a comma at the last attribute.

IE 在定义对象时“过敏”,并在最后一个属性处留一个逗号。

Bad:

坏的:

var apple = { color : "yellow",
              taste : "good", };

Good:

好的:

var apple = { color : "yellow",
              taste : "good" };

回答by Micah

You could use a try catch statement.

您可以使用 try catch 语句。

var style = label.style;

try 
{
    style.width = node.data.offsetWidth;
    style.height = node.data.offsetHeight;            
} 
catch(err) { /* do nothing */ }

style.cursor = 'pointer';
style.color = '#fff';
style.fontSize = '0.8em';
style.textAlign= 'center';

回答by Selvakumar Arumugam

Wrap those offending code inside a try { } catch (e) {}block and you should be good to go..

将那些有问题的代码包装在一个try { } catch (e) {}块中,你应该很高兴..

MDN Reference for try..catch

try..catch 的 MDN 参考

Something like below should work for you,

像下面这样的东西应该适合你,

var style = label.style;
try {
    style.width = node.data.offsetWidth;
    style.height = node.data.offsetHeight;            
} catch (e) { 
    //do alternate when error   
}
style.cursor = 'pointer';
style.color = '#fff';
style.fontSize = '0.8em';
style.textAlign= 'center';

回答by HellaMad

You can use try...catch:

您可以使用 try...catch:

try{
    allert('hello'); //Syntax error
}catch(err){
    console.log(err);
}

回答by Simon Smith

If you know that your code is likely to encounter an error (for whatever reason) you can catch and handle the errors with a try/catch:

如果您知道您的代码可能会遇到错误(无论出于何种原因),您可以使用 try/catch 来捕获和处理错误:

try {
    // Code that is likely to error
} catch(e) {
  // Handle the error here
}

You can either do nothing with the error or try to recover your application. In this case you should probably try to find out why IE is throwing an error in the first place and see if you can avoid having to suppress the errors.

您可以对错误不采取任何措施,也可以尝试恢复您的应用程序。在这种情况下,您应该首先尝试找出 IE 抛出错误的原因,看看是否可以避免抑制错误。

Further reading:

进一步阅读: