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?