简体   繁体   中英

How to call a JavaScript function using keyboard shortcut?

There is a website which consists of main frame and some additional ones (they're responsible for parsing inserted data). When I start to edit a record, new frame appears. In the source code I see some JavaScript scripts added. Some of them come from site directories, but also there are some defined in the <head> section of the frame. And there is a function defined, let's call it parseData(input){...} , which is then called upon pressing "Save" link.

Is there any way to assign a keyboard shortcut to execute saving? There are no ids defined for the "Save" link, only href and class .

I tried bookmarklets*, but was stuck on joining the frame URL and the JS function call. Any other ideas?

Edit: * bookmarklets are meant to be launched using Shortcut Manager-like web browser extension.

Edit2: I'm not developing the website. I have read-only access to it.

If you want to use pure javascript to bind, say, "enter" to the function parseData, you could use:

document.onkeydown = function(e){
    e = e || window.event;
    keycode = e.which || e.keyCode;
    if(keycode == 13){      // '13' is the keycode for "enter"
        e.preventDefault();
        parseData(input);
     }
}

You might want to use jQuery with this plugin to create keyboard shortcuts. Have a look at this:

https://github.com/jeresig/jquery.hotkeys

You may do something like this:

$(document).on('keydown', null, 'ctrl+s', saveFunction);

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