最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Ignore errors and continue running javascript in IE? - Stack Overflow

programmeradmin2浏览0评论

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.

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

    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';
},

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.

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

    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';
},
Share Improve this question asked Feb 22, 2012 at 15:27 RobRob 2,7795 gold badges24 silver badges34 bronze badges 2
  • 3 Posting the full error message you get will greatly help us to help you. – Shadow Wizard Commented Feb 22, 2012 at 15:29
  • related: stackoverflow.com/questions/12219154/… – Ciro Santilli OurBigBook.com Commented Sep 18, 2016 at 23:18
Add a comment  | 

6 Answers 6

Reset to default 10

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

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

Bad:

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

Good:

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

You could use a try catch statement.

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';

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

MDN Reference for try..catch

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';

You can use try...catch:

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

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 {
    // 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.

Further reading:

  • http://www.impressivewebs.com/javascript-try-catch/
  • https://developer.mozilla.org/en/JavaScript/Reference/Statements/try...catch
发布评论

评论列表(0)

  1. 暂无评论