javascript - Injected JS assigning window.onerror fails to fire -


i need run third party js scan jserrors. running following in chrome's console

window.onerror = function(errormsg, url, linenumber) {   alert(errormsg); }  throw new error('foo'); 

i expect both console inform me of error , alert pop displaying error message. alert never fired, , assume event not being fired. why not?

the chrome developer console won't call user-defined error event listener if error thrown (according this answer paul s.).

throwing error "just in time":

> window.onerror = function () {console.log('error!');}; function () {console.log('error!');} > throw new error(); error 

throwing error deferred:

> window.settimeout(function() {throw new error()}, 0); xxxx error! uncaught error 

(xxxx specific return value of settimeout())


here original code snippet led me point chrome dev console changes internal behaviour somehow:

// #btn simple button document.getelementbyid("btn").addeventlistener("click", function () {     var script = document.createelement("script");     script.innerhtml = "window.onerror=function(){alert('an error occurred!');};throw new error('42');";     document.head.appendchild(script); }); 

this code executed in normal html file.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -