Q:

Javascript: Earliest moment to add event listener to document object

I want to add a keydown event listener to the document object without JQuery.
In the listener I'm not accessing the DOM. The only thing I need from the DOM is the document object itself. I only want to disable pressing of certain key.
Is it safe to call it directly in the script (I mean when the script loads), or do I need to use onload callback (or maybe something else)?
Thanks in advance.

A:

The document object is always available wherever you place your script. No need to wait for DOMContentLoaded or anything; just add your listener whenever you like.
This is not true for elements within the document.

A:

You can just bind to the DOMContentLoaded event.  That is how jQuery does it.
Example:
document.addEventListener("DOMContentLoaded", function(){
    //Bind the key event here.
});

That doesn't work in IE.  So, for IE, you can do this:
document.attachEvent("DOMContentLoaded", function(){
    //Bind the key event here.
});

