7

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';
},
Rob
  • 2,779
  • 5
  • 23
  • 34

6 Answers6

10

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

DaveS
  • 3,156
  • 1
  • 21
  • 31
3

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" };
p1100i
  • 3,710
  • 2
  • 29
  • 45
  • I'll try this, but I'm pretty sure it's something with node.data.offsetWidth/Height, all the other lines are okay and wrapping it with try..catch worked. – Rob Feb 22 '12 at 15:46
  • Well yeah, it was just a guess because i saw that comman on your snippet. – p1100i Feb 22 '12 at 16:50
2

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';
Micah
  • 111,873
  • 86
  • 233
  • 325
1

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';
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
0

You can use try...catch:

try{
    allert('hello'); //Syntax error
}catch(err){
    console.log(err);
}
HellaMad
  • 5,294
  • 6
  • 31
  • 53
  • this will fail for another reason in IE < version 9 then, as `console.log` is not implemented in versions older 9. – Kaii Nov 07 '13 at 20:31
0

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:

Simon Smith
  • 8,024
  • 2
  • 30
  • 40