Bookmarklets: Doc JavaScript's Ad Bookmarklet - | WebReference

Bookmarklets: Doc JavaScript's Ad Bookmarklet -

Doc JavaScript's Ad Bookmarklet

Before we sum up this column, we would like to introduce a fun bookmarklet. Wherever you are, just execute it and the advertising banners on the page will be replaced with our own banner.

Go ahead and give it a try: Doc JavaScript Ads. Click the link to make it work on this page, or drag it to your Personal Toolbar (Navigator) or Links bar (Internet Explorer) for easy access wherever you are. Now let's take a look at the script behind the scenes:

<A HREF='javascript:void(imgs=document.images);for(i=0;i<imgs.length;i++)
src="");}'>Doc JavaScript Ads</A>
  // (The above three lines should be joined as one line.
  // They have been split for formatting purposes.)

First, notice the use of the void operator on statements that return explicit values (assignment statements, in our case). We assign the document.images array to a variable named imgs, and loops through all its elements. For each image, we check if its dimensions are 468x60 -- typical ad banner dimensions. If the condition evaluates to true, we change the source of the image (its src property) to, the URL of Doc JavaScript's banner.

Unlike our search engine bookmarklets, this bookmarklet doesn't affect frame-based Web pages, because images in such pages are included in the frames, and are not integrated in the parent window. Therefore, running a script in the parent window's Location bar (or Address bar, in Internet Explorer) does not influence the inner frames, because the specified document object belongs to the parent's window object. Bear in mind that the term "bookmarklet" refers to javascript: bookmarks, not just ordinary javascript: links.

Produced by Yehuda Shiran and Tomer Shiran

Created: February 1, 1999
Revised: February 1, 1999