简体   繁体   中英

Use key navigation script on more than one page

I am trying to do something like pressing keys A and H opens home page. That is the user has to press the H key on holding the A key. I want this event to be occurred across all the website not in a single page.

I came with a jQuery approach as below

var down = {};
$(document).keydown(function(e) {
    down[e.keyCode] = true;
}).keydown(function(e) {
      if (down[65] && down[72]) {
      // logic to go to my home page
      }
});​

When i implemented the above script in a single jsp page, it works fine. How can i make my script listen across all the web pages.

You should use a common header/footer sections for all pages or at least a common template.
Add the crucial js code into the common template to make it work on all pages. Avoid duplicating same code on each page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM