Why doesn't window.onerror handle errors in the Chrome DevTools console?
The question might seem silly, but I believe it’s not. This code block overrides the onerror
functionality in the window
object. The toString
function is changed to x
, and then the window
object is concatenated with a string, which forces the toString
method to be called on the window
object. This causes the x
function to run, and an alert should be triggered.
When I add this code inside <script>
tags in an HTML file, the alert works. However, when I paste the code into the developer console, an alert doesn’t show up. Instead, I get an uncaught error message.
Here’s the code:
x = e => { window.onerror = alert; throw "errorMessage"; }
toString = x;
window + "test";
Why does this happen?