Well, you set a global variable onclickthat translates into a global windowproperty object onclick. This is basically the same thing that you should install it directly:
window.onclick = function() {
console.log("x = " + ++x);
};
And since you set the click event for the entire object window, it will fire not only when the button is clicked, but also when you click on something inside your document (if the event is not stopped).
source
share