Bookmarklets: Doc JavaScript's Ad Bookmarklet - www.docjavascript.com | WebReference

Bookmarklets: Doc JavaScript's Ad Bookmarklet - www.docjavascript.com


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++)
{if((imgs[i].width==468)&&(imgs[i].height==60))void(imgs[i].
src="http://www.webreference.com/js/docjsad.gif");}'>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 http://www.webreference.com/js/docjsad.gif, 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.


http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

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

URL: http://www.webreference.com/js/column35/docjs.html