Hi all,

I want to know if it is possible and how to intercept an existing javascript keybind and block it.

For example, a website has some javascript that, when the user hits the letter "H" on the keyboard, the website automatically runs a function, e.g. redirecting to another web page.

I want to block this functionality after it has been loaded into the page.

Is this possible? Perhaps the stoppropagation function? Or maybe simply re-assign the key event to just run an empty function?

Thanks,
Max.

If your page contains a frame, and THAT frame contains and EXTERNAL/REMOTE page that contains the javascript that is "listening" for the "H" keypress, then you will NOT be able to rebind the event. In other words, what you are trying to do is not possible. The security restrictions imposed by the browser will not give you access to the iframe content/objects if it is from a different domain.

Thanks for your reply,

The process does not involve any iFrames. I am going to use this in a browser add-on and so the javascript will be inserted directly into the page as if I was the developer of that website and just inserting code as normal.

Max

then you will need to do so AFTER the page has loaded. To clarify, basically this is what needs to be done:

//assuming this is what is there initially
document.body.onkeypress=function(){
 //code that "listens" for "H"
 ...
};

then you need to override that listener after page load:

onload=function(){
 //assuming this is what is there initially
 document.body.onkeypress=function(){
  //YOUR function definition here.
  ...
 };
}

Iv had a go at that, the key I need to override is backspace but entering the backspace code (8) does not seem to work. How should that work?

.... browser add-on .....

?????

Thanks hielo for your post. It is really very appreciated.

Iv had a go at that, the key I need to override is backspace but entering the backspace code (8) does not seem to work. How should that work?

it will be better if you post what you have

@Airshow: I suspect he's developing something similar to a Greasemonkey script/"extension"

Hielo,

@Airshow: I suspect he's developing something similar to a Greasemonkey script/"extension"

Aha! Thanks, this topic makes sense now.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.