spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column35


Search Engine Bookmarklets

UNIX System Administrator - SUN Solaris, Veritas, EMC, Shell Scripting, SAN (NYC)
Next Step Systems
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
ActiveState Debuts Open Source Business Suite
Salesforce Offers Visual App Builder
Codesion Steps Out From CVS's Shadow


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


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Use Web Caching to Make Your Web Site Faster · Creating an Online Shopping Cart Mechanism in PHP · Log JavaScript Errors Using an AJAX-driven Web Service
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Configuring Granular Settings for a Database Level Audit · The Perils of a Web 2.0 Transition on Your Business Processes · Facebook Redesigns Site —Again — Nears 400M Mark


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

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