I have some code that runs when a user clicks anywhere in the body of the web page. I only want the JavaScript to run if the click is on a NON-LINK. Any ideas?
Thanks
Mike
I have some code that runs when a user clicks anywhere in the body of the web page. I only want the JavaScript to run if the click is on a NON-LINK. Any ideas?
Thanks
Mike
document.body.onclick = function(e){
var target = e ? e.target : window.event.srcElement;
if (target.nodeName.toLowerCase() !== 'a') {
// Do something here...
}
};
Note that any attempts to stop propagation before the event reaches the <body>
will stop the above handler from running. To avoid this you can use event capturing.
document.body.onclick = function... is a statement. Hence the closing ';'
Use jQuery.
$(function() {
$("body").click( function() { /* your code here */ } );
$("a").click( function(e) { e.stopPropagation(); } );
});