GO! <...">

Window.onerror get target element

how to get target element in event error in javascript?

Source:

<button onclick="a(this);">GO!</button>
<script>
window.onerror = function(message, source, lineno, colno, error) {
  console.log(error.stack); // event.target = window
}
</script>

of

ReferenceError: a not defined

in HTMLButtonElement.onclick (onerror.html: 1)

What is this "HTMLButtonElement.onclick"?

var domID = "HTMLButtonElement".id;
addLog(document.querySelector(domID)); //example usage
+4
source share
1 answer

Firstly, I highly recommend that you do not use the onclick built-in event handlers because of the problem you are facing - they are incredibly difficult to debug and do not encourage separation of problems (html is for markup, js files for logic).

-, onerror , , ( ) onerror. , " , . Array.map = function() {};

-, .

button.addEventListener('click', function() {
    try {
        a(this);
    } catch (e) {
        e.message = e.message + " - element clicked: " + this.id;
        throw e;
    }
});

JSFiddle.

0

All Articles