I was testing a piece of code. I had a function named write, and I used a button with the inline onclick handler to run write().
function write(text) {
alert(text)
}
<button onclick='write("Some text")'>
Write
</button>
To my surprise, write() actually executed document.write(). To be sure, I tested it with a few more functions.
function write(text) {
alert(text)
}
<button onclick='console.log(body)'>
document.body
</button>
Then, I wondered if they could access window. It turned out that they can. But, document.window is undefined.
console.log(document.window)
/*
Note that this example may not work due to StackSnippets using iframes
*/
<button onclick='window.open()'>
Open blank page
</button>
function doSomething(text) {
console.log(text)
}
<button onclick='doSomething("Some text")'>
doSomething
</button>
So, my question is, why can inline event handlers access properties of window, even though document is the global scope, and document.window is undefined?
In inline event handlers, if
this.propertyexists, it defaults to that. If not, it falls back todocument.property. If even that doesn't exist, finally, it executes that code in the globalwindowscope.Some examples
In this example, the inline handler defaults to
documentas the global scope.In this example, it defaults to
window.In yet another example, it defaults to
this, which is the element.