1
window.onerror = function(type, file, line){
        if(type) {
            console.log(type);
        }
        if(file) {
            console.log(file);
        }
        if(line) {
            console.log(line);
        }
    }

this code returns "Script error" when there is an error at some of the .js files. I need the type, file and line of the error. How can I get it? When window throws error this script works perfect but it is not the same when there is error in the .js file. I know that these things I can find on the console but imagine that I don't have one and i cannot install.

Nikolay
  • 185
  • 1
  • 5
  • 14
  • Possible duplicate of [Cryptic "Script Error." reported in Javascript in Chrome and Firefox](https://stackoverflow.com/questions/5913978/cryptic-script-error-reported-in-javascript-in-chrome-and-firefox) – Michael Freidgeim Apr 19 '19 at 01:45

3 Answers3

1

The post of Cryptic "Script Error." reported in Javascript in Chrome and Firefox should answer your "Script Error." problem. Namely it is probably caused by "Same origin policy".

Though I am still looking for why webkit will give me "undefined" file name and "0" line number for uncaught exception.

Community
  • 1
  • 1
weidongxu
  • 303
  • 1
  • 7
1
window.onerror = ErrorLog;
function ErrorLog (msg, url, line) {
    console.log("error: " + msg + "\n" + "file: " + url + "\n" + "line: " + line);
    return true; // avoid to display an error message in the browser
}
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
-1

Here's what I use to capture errors. I have it request an image whose url points to a server side script.

function logJSErrors(errObj) {
  if (!errObj || !errObj.lineNumber || errObj.lineNumber == 0) {
    return; // can't use it any way.
  }
  if (window.location && window.location.toString().indexOf('rfhelper32.js') >= 0) {
    return; // ignore the stupid Norton/Firefox conflict
  }

  var img = new Image();
  img.src = "/jserror?m=" + encodeURIComponent(errObj.message) +
      "&location=" + encodeURIComponent(window.location) +
      "&ln=" + encodeURIComponent(errObj.lineNumber) +
      "&url=" + encodeURIComponent(errObj.fileName) +
      "&browser=" + encodeURIComponent(errObj.browserInfo);
}

window.onerror = function (msg, url, line) {
  logJSErrors({ message : msg,
    lineNumber : line,
    fileName : url,
    browserInfo : window.navigator.userAgent
  });

  // if a jquery ajax call was running, be sure to make the spinning icons go away
  if (jQuery) {
    try {
      jQuery.event.trigger("ajaxStop");
    } catch(e) {/* do nothing */
    }
  }

};
Perry Tew
  • 2,632
  • 3
  • 22
  • 25
  • look at the call to logJSErrors in the window.onerror event. It contains all the error information. It's a hash. Why was this downvoted?? – Perry Tew Jan 13 '12 at 15:19
  • Again shows only "Script error" when I have problem in the javascript. This is not much different than my code and it is no use for me – Nikolay Jan 16 '12 at 07:59
  • Hmmm. This script works well for me. Would it be possible to post a link to your dev? Or put your page up on jsfiddle.net? At this point, I'd need to see why it's not working. – Perry Tew Jan 16 '12 at 14:01