AbreQueVoy
AbreQueVoy

Reputation: 2316

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.

Upvotes: 0

Views: 2379

Answers (2)

archlyfe
archlyfe

Reputation: 176

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);
     }
}

Upvotes: 3

Raouf Athar
Raouf Athar

Reputation: 1813

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);

Upvotes: 2

Related Questions