How to detect click (including browser's back button) events using javascript?
Asked
Active
Viewed 2.8k times
9
-
7+1 for a terribly worded question. – Apr 12 '10 at 12:12
-
6Two suggestions that will help you get better answers: 1. Re-word your question so that it's clear what it is you're talking about. Be as specific as you can. 2. Go back and accept answers for the 22 questions you've asked previously. People will be more likely to answer your questions if they know they're going to be recognized for it. – Syntactic Apr 12 '10 at 12:13
-
2No...! Javascript is innocent, don't sue it. :p – kennytm Apr 12 '10 at 12:22
-
3JavaScript *is* innocent. And JavaScript *will not rest* until the real event is detected and brought to justice. – Syntactic Apr 12 '10 at 12:40
1 Answers
21
You can detect clicks inside the page with a simple click
event handler:
document.onclick= function(event) {
// Compensate for IE<9's non-standard event model
//
if (event===undefined) event= window.event;
var target= 'target' in event? event.target : event.srcElement;
alert('clicked on '+target.tagName);
};
It is impossible for web-page script detect clicks outside the page such as the back button and other browser chrome, for good security reasons. You would have to be part of a browser extension to do this.

bobince
- 528,062
- 107
- 651
- 834