0

I have two test files, test1.html and test2.html. I have test1.html as follows:

<script>
window.onerror = function (message, file, lineNumber, columnNumber, error) {
    console.log(message, file, lineNumber, columnNumber, error);
};

asdf // Trigger error
</script>

which prints (as expected)

Uncaught ReferenceError: asdf is not defined file:///Users/justin/Desktop/test1.html 6 1 ReferenceError: asdf is not defined(…) 

I have test2.html as follows:

<script src='test.js'></script>

where test.js is:

window.onerror = function (message, file, lineNumber, columnNumber, error) {
    console.log(message, file, lineNumber, columnNumber, error);
};

asdf

which prints (not as expected)

Script error.  0 0 null

How can I catch the error information when my script is included using src='test.js' instead of inline?

Randomblue
  • 112,777
  • 145
  • 353
  • 547
  • see: http://stackoverflow.com/questions/5913978/cryptic-script-error-reported-in-javascript-in-chrome-and-firefox (the answer suggests: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-crossorigin) – Meiko Rachimow Mar 30 '16 at 22:01
  • I am doing this locally, and the `crossorigin` attribute gives me the error `Script from origin 'file://' has been blocked from loading by Cross-Origin Resource Sharing policy: Invalid response. Origin 'null' is therefore not allowed access.` – Randomblue Mar 30 '16 at 22:05
  • see http://stackoverflow.com/questions/8456538/origin-null-is-not-allowed-by-access-control-allow-origin – Meiko Rachimow Mar 30 '16 at 22:06
  • I am loading the scripts locally inside a `WKWebView`, so there's no web server for me, even in production. – Randomblue Mar 30 '16 at 22:10
  • or try: let the server return a 'Access-Control-Allow-Origin = null' header. https://issues.apache.org/jira/browse/CB-7348 – Meiko Rachimow Mar 30 '16 at 22:27
  • I'm not using Cordova. I'm using my own instance of `WKWebView` without server. – Randomblue Mar 30 '16 at 22:33
  • Cordova uses an embedded webserver: // Note: the embedded webserver is still needed for iOS 9. It's not needed to load index.html, // but we need it to ajax-load files (file:// protocol has no origin, leading to CORS issues). NSString *directoryPath = myMainViewController.wwwFolderName; https://github.com/Telerik-Verified-Plugins/WKWebView/blob/4f83a17a3e0cdecbe4cd2d2801625ab415aaa55f/src/ios/AppDelegate%2BWKWebViewPolyfill.m – Meiko Rachimow Mar 30 '16 at 22:37

1 Answers1

0

I did not use the window.onerror but it did show the same error:

Uncaught ReferenceError: asdf is not defined

try {
    asdf // Trigger error
} catch (e) {
    function test (message, file, lineNumber, columnNumber, error){
        console.log(message, file, lineNumber, columnNumber, error);
    };

    test(e);
}
Ray
  • 9,184
  • 3
  • 27
  • 44