Ram Ram - 8 months ago 156
Javascript Question

event.preventDefault not working everywhere?

i made a chrome extension to get element from web pages by using click event listener on content page but preventDefault is working for few times only.

for example on this website when i click on the menu bar it redirects to the next page instead of preventing click action to happen.
this is my event listener in content.js

document.addEventListener('click', function xyz(e){
var target = e.target || event.srcElement;
var attributes = Array.prototype.slice.call(target.attributes).map(function(i) {
return [String(i.name)+": "+String(i.value)]
prompt("xpath1 :",getPathTo(target));

how to stop click event from occuring !


You either need to use return false; or use e.preventDefault(); and e.stopPropagation() together. Because this will prevent event from bubbling up to the parent anchor tag.