Results 1 to 2 of 2

Thread: problem with using the same JS coding twice

  1. #1
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem with using the same JS coding twice

    Hello everybody,

    I have a nice search engine but its seems i can't put it twice on the same page:
    Code:
    <script language="JavaScript">
    
    function startSearch(){
    searchString = document.searchForm.searchText.value; 
    if(searchString != ""){
    searchEngine = document.searchForm.whichEngine.selectedIndex + 1;
    finalSearchString = "";
    
    if(searchEngine == 1){
    finalSearchString = "http://www.altavista.digital.com/cgi-bin/query?pg=q&what=web&fmt=.&q=" + searchString;
    }
    if(searchEngine == 2){
    finalSearchString = "http://av.yahoo.com/bin/query?p=" + searchString + "&hc=0&hs=0";
    }
    if(searchEngine == 3){
    finalSearchString = "http://www.excite.com/search.gw?trace=a&search=" + searchString;
    }
    if(searchEngine == 4){
    finalSearchString = "http://www.hotbot.com/?SW=web&SM=MC&MT=" + searchString + "&DC=10&DE=2&RG=NA&_v=2&act.search.x=89&act.search.y=7";
    }
    if(searchEngine == 5){
    finalSearchString = "http://www.infoseek.com/Titles?qt=" + searchString + "&col=WW&sv=IS&lk=noframes&nh=10";
    }
    if(searchEngine == 6){
    finalSearchString = "http://www.lycos.com/cgi-bin/pursuit?adv=%26adv%3B&cat=lycos&matchmode=and&query=" + searchString + "&x=45&y=11";
    }
    if(searchEngine == 7){
    finalSearchString = "http://netfind.aol.com/search.gw?search=" + searchString + "&c=web&lk=excite_netfind_us&src=1";
    }
    
    location.href = finalSearchString;
    }
    }
    
    
    // -->
    </script>
    
    <basefont face="Verdana, Arial, sans-serif">
    
    <form name="searchForm">
    
    <table width=320 border cellpadding=3 cellspacing=2 bgcolor=444444>
    
    <tr>
    <td bgcolor=lightblue><font size=1 face="Verdana, Arial, sans-serif">Search for:<br>
    <td bgcolor=lightblue><font size=1 face="Verdana, Arial, sans-serif">Search from:
    <td bgcolor=lightblue>&nbsp;
    
    <tr>
    <td bgcolor=navajowhite><input style="background: dddddd" name="searchText" type="text">
    <td bgcolor=navajowhite>
    <select style="background: dddddd" name="whichEngine">
    <option selected>Altavista
    <option>Yahoo!
    <option>Excite
    <option>Hotbot
    <option>Infoseek
    <option>Lycos
    <option>AOL Netfind
    </select>
    <td bgcolor=navajowhite><input type="button" value="Send" onClick="startSearch()">
    
    </select>
    </table>
    </form>

    Can someone tell me how to put the code twice in the html page without creating a conflict as a result of identical codes?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Your search engine URL's, like:

    Code:
    http://av.yahoo.com/bin/query?p=
    etc., appear to be incorrect. But this will get the script working with multiple forms (Note: Both the script code and the form(s) have changed):

    Code:
    <script type="text/javascript">
    function startSearch(form){
    var searchString = form.searchText.value;
    if(searchString != ""){
    var searchEngine = form.whichEngine.selectedIndex + 1,
    finalSearchString = "";
    
    if(searchEngine == 1){
    finalSearchString = "http://www.altavista.digital.com/cgi-bin/query?pg=q&what=web&fmt=.&q=" + searchString;
    }
    if(searchEngine == 2){
    finalSearchString = "http://av.yahoo.com/bin/query?p=" + searchString + "&hc=0&hs=0";
    }
    if(searchEngine == 3){
    finalSearchString = "http://www.excite.com/search.gw?trace=a&search=" + searchString;
    }
    if(searchEngine == 4){
    finalSearchString = "http://www.hotbot.com/?SW=web&SM=MC&MT=" + searchString + "&DC=10&DE=2&RG=NA&_v=2&act.search.x=89&act.search.y=7";
    }
    if(searchEngine == 5){
    finalSearchString = "http://www.infoseek.com/Titles?qt=" + searchString + "&col=WW&sv=IS&lk=noframes&nh=10";
    }
    if(searchEngine == 6){
    finalSearchString = "http://www.lycos.com/cgi-bin/pursuit?adv=%26adv%3B&cat=lycos&matchmode=and&query=" + searchString + "&x=45&y=11";
    }
    if(searchEngine == 7){
    finalSearchString = "http://netfind.aol.com/search.gw?search=" + searchString + "&c=web&lk=excite_netfind_us&src=1";
    }
    
    location.href = finalSearchString;
    }
    }
    </script>
    
    <basefont face="Verdana, Arial, sans-serif">
    
    <form action="#" onsubmit="startSearch(this);return false;">
    
    <table width=320 border cellpadding=3 cellspacing=2 bgcolor=444444>
    
    <tr>
    <td bgcolor=lightblue><font size=1 face="Verdana, Arial, sans-serif">Search for:<br>
    <td bgcolor=lightblue><font size=1 face="Verdana, Arial, sans-serif">Search from:
    <td bgcolor=lightblue>&nbsp;
    
    <tr>
    <td bgcolor=navajowhite><input style="background: #ddd" name="searchText" type="text">
    <td bgcolor=navajowhite>
    <select style="background: #ddd" name="whichEngine">
    <option selected>Altavista
    <option>Yahoo!
    <option>Excite
    <option>Hotbot
    <option>Infoseek
    <option>Lycos
    <option>AOL Netfind
    </select>
    <td bgcolor=navajowhite><input type="submit" value="Send">
    
    </select>
    </table>
    </form>
    
    <form action="#" onsubmit="startSearch(this);return false;">
    
    <table width=320 border cellpadding=3 cellspacing=2 bgcolor=444444>
    
    <tr>
    <td bgcolor=lightblue><font size=1 face="Verdana, Arial, sans-serif">Search for:<br>
    <td bgcolor=lightblue><font size=1 face="Verdana, Arial, sans-serif">Search from:
    <td bgcolor=lightblue>&nbsp;
    
    <tr>
    <td bgcolor=navajowhite><input style="background: #ddd" name="searchText" type="text">
    <td bgcolor=navajowhite>
    <select style="background: #ddd" name="whichEngine">
    <option selected>Altavista
    <option>Yahoo!
    <option>Excite
    <option>Hotbot
    <option>Infoseek
    <option>Lycos
    <option>AOL Netfind
    </select>
    <td bgcolor=navajowhite><input type="submit" value="Send">
    
    </select>
    </table>
    </form>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •