Suppress “Done, but with errors on page” in IE

2019-09-20 06:23发布

问题:

I have a website using lots of jQuery and JavaScript that produces a "Done, but with errors on page" message in the footer of IE. Everything on the site works perfectly, so I don't want to spend the time troubleshooting the exact error. All I would like to do is suppress the "Done, but with errors on page" message so that clients don't freak out.

I tried the following at the top of the page with no success:

window.onerror = function() {return true;}

回答1:

You can't stop the browser from telling users that there's errors on the page. That would be a security hole, and you don't really have the right to. You have no choice but to try to fix it or have users see the message.

Does this mean you should try and fix the error? Well, seeing as you can't suppress it, and you are correct in thinking that users won't enjoy the message, and that there are tons of excellent debugging tools available, there's really no reason not to.

Heed the warnings your tools give you, I've heard on the streets that the people who wrote them were pretty smart cookies. :D



回答2:

Have you tried using Firebug in Firefox to help you find out what the error is?

Firebug is a great javascript tool that gives a whole lot more information about the cause of javascript errors



回答3:

You should use the IE developer tools to debug the error.



回答4:

Code properly or use ugly hacks

try{
 // all your buggy code
} catch(e){
 // please don't do this - instead fix your code.
}


回答5:

The Javascript Error object contains different fields for different broswers:

in IE you have access to Error.name, Error.message, Error.number, and Error.description.

in FF you have access to Error.name, Error.message, Error.fileName, Error.lineNumber, and Error.stack (stack trace, shows "@" + Error.Filename + ":" + Error.lineNumber).

try {
  // all your buggy code
} catch (e) {
  // in FF you can add e.lineNumber or e.stack
  alert("Error: " + e.name + " - " + e.message); 
}

you can use this in IE, but unfortunately you can't trace it to the line number. For narrowing down the offending code in IE, add several "markers" in your code:

var myObject = new Object();
myObject.prototype.myFunction = function() {
  alert('start');
  // some code here
  alert('1');
  // some more code here
  alert('2');
  // some more code here
  alert('3');
  // some more code here
  alert('done');
} 

as your script loads, watch for the "Done with errors" icon as you dismiss the alerts. You can then narrow down where in your code the error is being thrown (i.e. "The error is thrown after '2' but before '3'..."). This is definitely a tedious process, but it's tried and true, and more importantly will leave your code bug-free.