views:

201

answers:

2

I use the window.onerror to alert javascript errors for debugging.

window.onerror = function(msg, url, line) {
    alert(msg + '\nLine: ' + line);
};

When an error is fired, it can alert this actual error message in IE. But in firefox, it just alerts "Script error!", but I can still see the actual error message in firefox's error console.

I remembered several months ago when I worked on another project, firefox did not work like this. But I cannot get the code of that project currently. So I wonder what are the possible problems with this?

+1  A: 
window.onerror(function(msg, url, line) {

You mean:

window.onerror= function(msg, url, line) {
bobince
sorry it was a typo.
powerboy
A: 

I know the answer now. After I uploaded the file to a webserver (localhost actually), visited it through http://localhost/path/to/the/file.html, the window.onerror event works as expected (as in IE, alerting the actual error message instead of just "Script error"). But it does not work when visiting the file locally through file:///D:/path/to/the/file.html

Cannot figure out why?

powerboy
IE doesn't run JavaScript on files from the filesystem by default. Turn on Internet Options->Advanced->‘Run active content from My Computer’ for testing on the filesystem, or add the “Mark of the Web”. http://msdn.microsoft.com/en-us/library/ms537628%28VS.85%29.aspx
bobince
yes, probably there is something to do with the security setting. But IE always alert the actual error message no matter "Run active content from My Computer" is on or off. But firefox just alert "sxript error" instead of the actual error message for files on local filesystem.
powerboy
Works for me. Let's have a complete test case.
bobince