954,566 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Intercept/block existing javascript keybind

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.

MaxMumford
Posting Whiz in Training
228 posts since Oct 2006
Reputation Points: 32
Solved Threads: 3
 

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.

hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 244
 

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

MaxMumford
Posting Whiz in Training
228 posts since Oct 2006
Reputation Points: 32
Solved Threads: 3
 

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.
  ...
 };
}
hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 244
 

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?

MaxMumford
Posting Whiz in Training
228 posts since Oct 2006
Reputation Points: 32
Solved Threads: 3
 
.... browser add-on .....


?????

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

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

sheva249
Newbie Poster
12 posts since Jan 2010
Reputation Points: 9
Solved Threads: 1
 
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
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 244
 

Hielo,
@Airshow: I suspect he's developing something similar to a Greasemonkey script/"extension"
Aha! Thanks, this topic makes sense now.Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: