English 中文(简体)
字面错误——类型、线和档案
原标题:Catch script error - type,line and file
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.

问题回答
window.onerror = ErrorLog;
function ErrorLog (msg, url, line) {
    console.log("error: " + msg + "
" + "file: " + url + "
" + "line: " + line);
    return true; // avoid to display an error message in the browser
}

这里我用来描述错误。 我要求有一个图像,该图像的斜向服务器的侧面书写。

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 */
    }
  }

};




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签