Iframe ad killer

Most ads I’ve seen are inside an iframe. That makes them very easy to target and murder with a bit of JavaScript. Here’s a small bookmarklet that will do just that:

iframe killer

Drag that bookmarklet (link) to your bookmarks toolbar, and click it whenever you see too many flash or image banners on the page.

Mind you, this also kills legitimate iframes, so it’s not 100% clean. It will also miss any ads that are not inside iframes.

This bookmarklet is provided to you on works-for-me basis, no guarantees, and no warranties. ;)

How it works is very simple. It fetches a list of all iframes in a document:

t = document.getElementsByTagName('iframe');

Then it loops through them (in reverse for speed), and clears the src attribute:

for(i = t.length;i;--i) {
  t[i-1].src='';
}

(Keep in mind that removing the iframes breaks the page layout, which is not what we want. Clearing the src attribute is enough.)

The code is then wrapped in a closure so that it doesn’t leak stuff (if it returns anything other than undefined, even by accident, the browser will try to go to the ‘URL’ defined by that something).

(function() {
  t = document.getElementsByTagName('iframe');
  for(i = t.length;i;--i) {
    t[i-1].src='';
  }
}());

Now, to make a bookmarklet of this, just strip all spaces, and prepend javascript: (see the bookmarklet link at the beginning of the article).