does anyone knows why doesn’t the onload event binds the this variable as expected?
Like in this test script, onkeydown gives us true (this is working fine) but onload gives us false (this now points to window.. wth?):
<!DOCTYPE html>
<html><head>
<script>
function f(t){
alert(t===document.body);
}
</script>
</head><body onkeydown="f(this);" onload="f(this);">
</body></html>
See the documentation:
The important part is this handler is triggered when the event reaches the window. The handler is executed in the context of the
windowand not thebody.FWIW,
alert(window.onload === document.body.onload);will yieldtrue.