Wednesday, June 28, 2006

Search for Selected Text

Highlight text in any web page and search for it on Google with the click of a button.

Start by creating a new button on your browser's links bar and naming it "Search Google" or something similar.

Next, if you’re using Mozilla, Firefox, or Netscape, place this JavaScript code (all on one line) into the button’s location field:
javascript: s = document.getSelection( );
for(i = 0; i < frames.length; i++) {
s = frames[i].document.getSelection( );
}
location.href = 'http://www.google.com/search?q='+escape(s);
If you’re using Internet Explorer, you’ll need this code:
javascript: s = (document.frames.length ? ' ':
document.selection.createRange( ).text);
for(i = 0; i < document.frames.length; i++) {
s=document.frames[i].document.selection.createRange( ).text;
}
location.href = 'http://www.google.com/search?q='+escape(s);
Next, open any web page, highlight some text, and click your new search button. The next thing you’ll see is a search results page with items matching your selected text.

Bug: This doesn't work when there are frames in the webpage.