Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

SearchLite

0.00/5 (No votes)
23 May 2002 1  
search multiple sites at once

Introduction

I regularly spend a lot of time searching for smart answers on the Net at multiple sites. Lazy that I am, I find typing keywords a pain in the carpals so I wrote this cheeky JavaScript to make my life easier. This piece of code is just an old trick with a twist

Information can be posted using a HTML Form through the GET or POST method. If a site uses the GET method, the values that are submitted by the user are visible in the URL. Most Search Engines use the GET method and this allows other developers to use it in their own applications . The world's fastest Browser, Opera , has a search field in the Progress bar of any window that is opened, that allows using various Search Engine sites individually, without actually visiting the homepage. It takes advantage of this particular QueryString feature to dynamically build a URL based on the Site and the Keyword specified.

For instance by placing a keyword at the end of this URL http://www.google.com/search?hl=en&btnG=Google+Search&q= , I can search on Google without having to actually visit their homepage. HTML cannot handle certain special characters and so care must be taken to escape them so that the URL is a valid one and the parameters are properly received. JavaScript has a function called escape() which does the job.

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: * @ - _ + . / 

By noticing the URL of other Search engines this script can be customized to handle many other sites which use the GET method to accept search keywords. I would be interested in hearing about other great sites of general interested which can be included here.

Be aware however that on clicking the Find button, as many windows as the number of checked boxes are opened. So here comes the meat of the code.

Code Listing

<script type="text/javascript">
function win()
{
    //this function finds out which sites the 

    //user intends to search

    //Based on which sites are checked, the URL 

    //is dynamically built with the keyword specified

    //The window.open method uses the dynamically 

    //created string as a URL to open a new window 

    frmlen = document.myform.elements.length;
    for (var icnt=0;icnt<frmlen;icnt++)
    {
        if (document.myform.elements[icnt].type=="checkbox" 
            && document.myform.elements[icnt].checked)
        {
            var strSite = document.myform.elements[icnt].value;
            strSearch= escape(document.myform.mytext.value);
            var url = strSite + strSearch;
            window.open(url);
        }

    }
}
</script>

and here's the HTML

<form method='post' name='myform'>
<table border=0 align='center'>
    <tr>
        <td colspan=5 height='10%'>
        SEARCHLITE - search multiple sites at once 
        </td>
    </tr>
    <tr>
        <td>* opens new windows</td>
    </tr>
    <tr >
        <td COLSPAN=3 height='20%'>
        <input type="text" name="mytext" size="55">
        </td>
        <td COLSPAN=2>
        <input type="button" value="Find" onClick="win()"><input type="reset" value="Reset">
        </td>
    </tr>
    <tr >
        <td>
        <input type="checkbox" value="http://www.google.com/
            search?hl=en&btnG=Google+Search&q=">
        Google</td>
        <td>
        <input type="checkbox" value="http://www.dictionary.com/
            search?q=">
        Dictionary</td>
        <td>
        <input type="checkbox" value="http://www.thesaurus.com/
            cgi-bin/search?config=roget&words=">
        Thesaurus</td>
        <td>
        <input type="checkbox" value="http://groups.google.com/
            groups?q=">
        Google Groups</td>
    </tr>
    <tr>
        <td> 
        <input type="checkbox" value="http://www.codehound.com/
            asp/results/results.asp?Q=">
        CodeHound ASP</td>
        <td>
        <input type="checkbox" value="http://www.codehound.com/
            js/results/results.asp?Q=">
        CodeHound JS</td>
        <td>
        <input type="checkbox" value="http://www.codehound.com/
            vb/results/results.asp?Q=">
        CodeHound VB</td>
        <td>
        <input type="checkbox" value="http://www.codehound.com/
            xml/results/results.asp?Q=">
        CodeHound XML</td>
    </tr>
    <tr>
        <td>
        <input type="checkbox" value="http://www.amazon.com/exec/
            obidos/external-search/102-3070862-5093731?
            mode=blended&tag=lexico&keyword=">
        Amazon</td>
        <td>
        <input type="checkbox" value="http://www.acronymfinder.com/
            af-query.asp?p=dict&String=exact&Acronym=">
        AcronynmFinder</td>
        <td>
        <input type="checkbox"  value="http://www.ucc.ie/
            cgi-bin/acronym?">
        Acronyms</td>
        <td>
        <input type="checkbox" value="http://www.irt.org/cgi-bin/
            htwrap?method=and&format=builtin-long&sort=
            score&config=htdig&restrict=&exclude=&words=">
        IRT.org</td>
    </tr>
    <tr>
</table>
</form>

Check out SearchLite v2 if you'd like your results to be served on a single page.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here