Rob
Rob

Reputation: 2779

Ignore errors and continue running javascript in IE?

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

Upvotes: 7

Views: 16427

Answers (6)

Simon Smith
Simon Smith

Reputation: 8044

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:

Upvotes: 0

HellaMad
HellaMad

Reputation: 5374

You can use try...catch:

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

Upvotes: 0

Micah
Micah

Reputation: 116050

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

Upvotes: 2

p1100i
p1100i

Reputation: 3740

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" };

Upvotes: 3

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

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

Upvotes: 1

DaveS
DaveS

Reputation: 3294

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

Upvotes: 10

Related Questions