Javascript window.onerror

3054
2
Jump to solution
06-26-2015 10:00 AM
LauraMiles1
Occasional Contributor III

I'm trying to have an alert popup whenever javascript logs an error. Am I correct in using the code:

window.onerror = function (errorMsg, url, lineNumber) {
    alert('Error: ' + errorMsg + ' Script: ' + url + ' Line: ' + lineNumber);
};

This seems not to produce an alert box. Currently my map is creating a 401 error for a layer every time it loads, as well as an error for a missing favicon, so something should be popping up in the alert box. I've tried putting this code at the start of my javascript file before any other code, inside a function to update layers when they are turned on and off, and inside a function that runs on map load...nothing happens. I'm still learning my way around Javascript so not sure where I'm going wrong.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Laura,

  I think because the layer object has an on error event already that it does not bubble to the window object.

  I can get this to work for a layer error:

        dynamicMapServiceLayer.on("error", function(error){
          alert('Error: ' + error.error);
        });

View solution in original post

2 Replies
RobertScheitlin__GISP
MVP Emeritus

Laura,

  I think because the layer object has an on error event already that it does not bubble to the window object.

  I can get this to work for a layer error:

        dynamicMapServiceLayer.on("error", function(error){
          alert('Error: ' + error.error);
        });
LauraMiles1
Occasional Contributor III

That works - thank you Robert!

0 Kudos