spacer
Yehuda Shiran December 15, 1999
Search Engine Bookmarklets
Tips: December 1999

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

Search engine bookmarklets enable you to query a search engine without having to load the engine's home page. When you execute such a bookmarklet, a prompt box asks you for the desired keywords, and the search engine's results are immediately retrieved. You don't have to load the search engine's home page. If you're using a fourth-generation browser, go ahead and follow one of these links (be sure to come back after you've been impressed):

Altavista
Excite
Infoseek
Lycos

Here are the URLs of the above bookmarklets:

<A HREF='javascript:void(str=prompt("Search for:",""));if(str){location.href="http://www.altavista.com/cgi-bin/query?pg=q&kl=XX&q="+escape(str).split("%20").join("+");}'>Altavista</A><BR>
<A HREF='javascript:void(str=prompt("Search for:",""));if(str){location.href="http://search.excite.com/search.gw?search="+escape(str).split("%20").join("+");}'>Excite</A><BR>
<A HREF='javascript:void(str=prompt("Search for:",""));if(str){location.href="http://infoseek.go.com/Titles?qt="+escape(str).split("%20").join("+")+"&col=WW&sv=IS&lk=noframes";}'>Infoseek</A><BR>
<A HREF='javascript:void(str=prompt("Search for:",""));if(str){location.href="http://www.lycos.com/cgi-bin/pursuit?matchmode=and&cat=lycos&query="+escape(str).split("%20").join("+");}'>Lycos</A>

First, we ask the user for input via the prompt() method. The input is stored in a variable named str, but the statement itself doesn't evaluate to the value of str thanks to the void operator. The second argument of the prompt() method, an empty string, specifies that the prompt box should initially be empty. If the user entered a value in the prompt box, we load the search engine's result page, along with the user's input.

If you search Altavista for the words "Tomer Shiran," the URL of the result page is:

http://www.altavista.com/cgi-bin/query?pg=q&kl=XX&q=Tomer+Shiran

But since we want to use the user's keywords, we need to replace the string Tomer+Shiran with the user's input:

escape(str).split("%20").join("+")

We use one built-in function, escape(), and two built-in methods, split() and join(), to encode the user's input. The escape() function encodes special characters in the specified string and returns the new string. It encodes spaces, punctuation, and any other character that is not an ASCII alphanumeric character, with the exception of these characters: * @ - _ + . /. These characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is converted to %20.

The URL of the search engine's result page is encoded in the same way, with one exception: spaces are converted to + characters. The combination of split() and join() is used to replace all appearances of %20 with + in the encoded string.

Now that you know how these bookmarklets work, be sure to pick them up and add them to your browser as fresh new bookmarklets.

For more information on the javascript: protocol, refer to Column 35, Bookmarklets.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint