2012-01-13 2 views
1
window.onerror = function(type, file, line){ 
     if(type) { 
      console.log(type); 
     } 
     if(file) { 
      console.log(file); 
     } 
     if(line) { 
      console.log(line); 
     } 
    } 

일부 .js 파일에 오류가 있으면이 코드는 "스크립트 오류"를 반환합니다. 오류의 유형, 파일 및 라인이 필요합니다. 그것을 어떻게 얻을 수 있습니까? 창에 오류가 발생하면이 스크립트는 완벽하게 작동하지만 .js 파일에 오류가있는 경우에는 동일하지 않습니다. 나는 콘솔에서 찾을 수있는 이러한 것들을 알고 있지만 내가 가지고 있지 않으며 설치할 수 없다고 상상한다.캐치 스크립트 오류 - 유형, 라인 및 파일

답변

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 
} 
+0

귀하의 코드와 내가 인용 한 것과별로 다르지 않습니다. – Nikolay

-1

다음은 오류를 캡처하는 방법입니다. 나는 url이 서버 측 스크립트를 가리키는 이미지를 요청해야한다.

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

}; 
+0

window.onerror 이벤트에서 logJSErrors에 대한 호출을 살펴보십시오. 여기에는 모든 오류 정보가 들어 있습니다. 해시예요. 왜 이것 downvoted했다 ?? –

+0

자바 스크립트에서 문제가 발생하면 다시 "스크립트 오류"만 표시됩니다. 이건 내 코드와 크게 다르지 않고, 내게 쓸모가 없다. – Nikolay

+0

흠. 이 스크립트는 나를 위해 잘 작동합니다. 개발자에게 링크를 게시 할 수 있습니까? 아니면 jsfiddle.net에 페이지를 올리시겠습니까? 이 시점에서 왜 작동하지 않는지 알아야합니다. –

1

Cryptic "Script Error." reported in Javascript in Chrome and Firefox의 게시물은 귀하의 "스크립트 오류"에 응답해야합니다. 문제. 즉 그것은 아마도 "같은 출처 정책"에 기인 한 것입니다.

아직 웹킷에서 "정의되지 않은"파일 이름과 캐치되지 않은 예외에 대한 "0"행 번호를 제공하는 이유를 찾고 있습니다.