December 15, 1999 - Search Engine Bookmarklets | WebReference

December 15, 1999 - Search Engine Bookmarklets

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

Yehuda Shiran, Ph.D.
Doc JavaScript

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="https://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="https://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="https://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="https://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:

https://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.