Bookmarklets: Search Engine Bookmarklets - www.docjavascript.com | WebReference

Bookmarklets: Search Engine Bookmarklets - www.docjavascript.com


Search Engine Bookmarklets

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


<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>
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)
<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>
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)
<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>
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)
<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>
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)

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.


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/search.html