Javascript that prevents a specific url from loading?

Is there any javascript that prevents the link from loading on the page like: ads.link.com, www.link.com?

Does anyone know if this is possible?

event.preventDefault();

      

+3


source to share


1 answer


I wrote this Jsfiddle for you. Try it with code.

Url: http://jsfiddle.net/Lexjdgme/

HTML:

<a href="http://news.google.com">news.google.com</a><p>
<a href="http://www.google.com">www.google.com</a>

      

JavaScript:

[].forEach.call(document.getElementsByTagName('a'), function (el) {
  el.addEventListener('click', function(e) {
      if(e.target.href==="http://news.google.com"){
         e.preventDefault();
      }
  }, false);
});

      




EDIT:

If you want to remove the link tag, you can do it like this:

// With JQuery
$('.advise').remove();

// Or directly with javascript
var advise= document.querySelector('.advise');
advise.parentNode.removeChild(advise);

      

Another Jsfiddle: http://jsfiddle.net/Lexjdgme/1/

+2


source







All Articles