

remember IE can't capture on the window before IE9 on keypress. Var el = window //we identify the element we want to target a listener on Var key = event.key || event.which || event.ke圜ode //find the key that was pressed whatever we want to do goes in this blockĮvent = event || window.event //capture the event, and ensure we have an event Now, as to how to actually capture the keystroke, let's do it in three parts: Part 2: Capturing all keyboard events on the window:

So that answers the question of "where do I put the codeblock, in the ?": No. The only things you might want to load at the head of the document are libraries like jQuery, because those are widely cached, especially if you're using a CDN version of jQuery, as that generally tends to not impact load times. If this doesn't make sense say something, and an article shall be found).īut here's something for you to consider: Good javascript programmer mentors today recommend all javascript be loaded at the end of the page. But if you want to capture a DOM element, then you have to execute the code after the DOM element occurs (because the script is interpreted as it's found, if the DOM element doesn't exist yet, the selector engine can't find it. To capture over the entire page, like as a page-help-function (maybe you want to capture F1?) then you would put your script block in the tag, inside a script.
