Advanced Search

Results 1 to 5 of 5

Thread: Javascript Search works in Firefox but not Internet Explorer - any takers?

  1. #1
    Join Date
    Sep 2008
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Javascript Search works in Firefox but not Internet Explorer - any takers?

    PROBLEM: Have a simple Javascript search engine which works in Firefox 2.0.0.16 but not in Internet Explorer 7.0.5730.13

    QUESTION: Prior to the whole world just using FF I of course, I want this baby to also work in IE - well, as many browser types & versions as possible really but at least FF2 & IE 7 onwards. Of course I've tried to poke around and hack this to do that but to no avail...
    I have a sinking feeling that it is going to be something simple like some ";" I've missed somewhere - I am however looking forward to someone with a better eye for detail seeing this & pointing it out in record time
    So thanks in advanced!

    BACKGROUND: This script basically allows the user to type in a search term & get a ranked list of webpages which may be most relevant to their needs. Cut & paste it into a test.html & try it out in FF first then in IE and you'll see what I mean...

    Thanks in advanced for a possibly simple fix!

    Code:
    <head>
    <SCRIPT LANGUAGE="JavaScript">
    var item = new Array();
    // "Page Name","path","Page Title","Many,Key,Words","Descriptive Comments"
    c=0; item[c]=new Array("helpIndex.shtml","","Help Home","Help Overview. ","Overview of Help as well as how to use it.");
    c++; item[c]=new Array("overviewAdmin.shtml","","Administrative Task Overview","Administrative tasks blah blah blah","Administrative tasks blah blah blah");
    c++; item[c]=new Array("links.htm","","Links page","links,more,where,similar,friends","Links to my favourite sites which I find interesting. Other friends sites which have similar interests to my own.");
    c++; item[c]=new Array("main.htm","main/","Main Page","content,main,focus","The main part of my site which contains what you have come to see. Lots of stuff like that and more great things. All in a sub directory.");
    c++; item[c]=new Array("logo.jpg","main/images/","Link Logo","link,image,logo,graphic","The logo.jpg is just a small image which you can place on your site as a link to me. It's in a second level subdirectory. site which contains what you have come to see. Lots of stuff like that and more great things. All in site which contains what you have come to see. Lots of stuff like that and more great things. All in site which contains what you have come to see. Lots of stuff like that and more great things. All in site which contains what you have come to see. Lots of stuff like that and more great things. All in site which contains what you have come to see. Lots of stuff like that and more great things. All in site which contains what you have come to see. Lots of stuff like that and more great things. All in site which contains what you have come to see. Lots of stuff like that and more great things. All in site which contains what you have come to see. Lots of stuff like that and more great things. All in ");
    
    page="<html><head><title>Search Results</title></head><body bgcolor='white'><center><table border=0>";
    
    function search(frm) {
    document.write(page);
    txt = frm.srchval.value.split(" ");
    fnd = new Array(); total=0;
    for (i = 0; i < item.length; i++) {
    fnd[i] = 0; order = new Array(0, 4, 2, 3);
    for (j = 0; j < order.length; j++)
    for (k = 0; k < txt.length; k++)
    if (item[i][order[j]].toLowerCase().indexOf(txt[k]) > -1 && txt[k] != "")
    fnd[i] += (j+1);
    }
    for (i = 0; i < fnd.length; i++) {
    n = 0; w = -1;
    for (j = 0;j < fnd.length; j++)
    if (fnd[j] > n) { n = fnd[j]; w = j; };
    if (w > -1) total += show(w, n);
    fnd[w] = 0;
    }
    document.write("<tr><td><br/><font face=arial size=2 color=#999999>Total found: "+total+"</font></td></tr><tr><td bgcolor=#B3B5FF><a href=\"javascript:history.go(-1)\"><font face=arial>Search Again</font></a></td></tr><tr><td bgcolor=#696DFF><a href=\"helpIndex.shtml\"><font face=arial>Return to Help Home</font></a></td></tr></table></body></html>");
    document.close();
    }
    function show(which,num) {
    link = item[which][1] + item[which][0]; 
    line = "<tr><td><font face=arial><a href='"+link+"'>"+item[which][2]+"</a> <font size=1 color=#999999>Relevance Score: "+num+"</font></font><br>";
    //line += item[which][4] + "<br>"+link+"</td></tr>";
    line += item[which][4] + "</td></tr>";
    document.write(line);
    return 1;
    }
    </script>
    </HEAD>
    
    <body>
    
    <center>
    <br/>
    <form method=get action="javascript:void(0)" onsubmit="search(this); return false;">
    <tr><td><input type=text name=srchval value=""><input type=submit value="Search"></td></tr>
    </form>
    </center>
    </body>

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    I've made some small changes in your source and the script started to work both in Mozilla Firefox, IE 7 and IE 8 Beta 2

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <script type="text/javascript">
            var item = new Array();
            // "Page Name","path","Page Title","Many,Key,Words","Descriptive Comments"
            c = 0;
            item[c] = new Array("helpIndex.shtml", "", "Help Home", "Help Overview. ", "Overview of Help as well as how to use it.");
            c++;
            item[c] = new Array("overviewAdmin.shtml", "", "Administrative Task Overview", "Administrative tasks blah blah blah", "Administrative tasks blah blah blah");
            c++;
            item[c] = new Array("links.htm", "", "Links page", "links,more,where,similar,friends", "Links to my favourite sites which I find interesting. Other friends sites which have similar interests to my own.");
            c++;
            item[c] = new Array("main.htm", "main/", "Main Page", "content,main,focus", "The main part of my site which contains what you have come to see. Lots of stuff like that and more great things. All in a sub directory.");
            c++;
            item[c] = new Array("logo.jpg", "main/images/", "Link Logo", "link,image,logo,graphic", "The logo.jpg is just a small image which you can place on your site as a link to me. It's in a second level subdirectory. site which contains what you have come to see. Lots of stuff like that and more great things. All in site which contains what you have come to see. Lots of stuff like that and more great things. All in site which contains what you have come to see. Lots of stuff like that and more great things. All in site which contains what you have come to see. Lots of stuff like that and more great things. All in site which contains what you have come to see. Lots of stuff like that and more great things. All in site which contains what you have come to see. Lots of stuff like that and more great things. All in site which contains what you have come to see. Lots of stuff like that and more great things. All in site which contains what you have come to see. Lots of stuff like that and more great things. All in ");
            
            page = "<html><head><title>Search Results</title></head><body bgcolor='white'><center><table border=0>";
            
            function search(frm){
                //document.write(page); //Comment this line
                txt = frm.srchval.value.split(" ");
                fnd = new Array();
                total = 0;
                for (i = 0; i < item.length; i++) {
                    fnd[i] = 0;
                    order = new Array(0, 4, 2, 3);
                    for (j = 0; j < order.length; j++) 
                        for (k = 0; k < txt.length; k++) 
                            if (item[i][order[j]].toLowerCase().indexOf(txt[k]) > -1 && txt[k] != "") 
                                fnd[i] += (j + 1);
                }
                for (i = 0; i < fnd.length; i++) {
                    n = 0;
                    w = -1;
                    for (j = 0; j < fnd.length; j++) 
                        if (fnd[j] > n) {
                            n = fnd[j];
                            w = j;
                        };
                    if (w > -1) 
                        total += show(w, n);
                    fnd[w] = 0;
                }
                document.write(page + "<tr><td><br/><font face=arial size=2 color=#999999>Total found: " + total + "</font></td></tr><tr><td bgcolor=#B3B5FF><a href=\"javascript:history.go(-1)\"><font face=arial>Search Again</font></a></td></tr><tr><td bgcolor=#696DFF><a href=\"helpIndex.shtml\"><font face=arial>Return to Help Home</font></a></td></tr></table></body></html>");
                document.close();
            }
            
            function show(which, num){
                link = item[which][1] + item[which][0];
                line = "<tr><td><font face=arial><a href='" + link + "'>" + item[which][2] + "</a> <font size=1 color=#999999>Relevance Score: " + num + "</font></font><br>";
                //line += item[which][4] + "<br>"+link+"</td></tr>";
                line += item[which][4] + "</td></tr>";
                //document.write(line); //Comment this line
    		  page += line; //Added this line newly here
                return 1;
            }
            </script>
        </head>
        <body>
            <center>
                <br/>
                <form method=get action="javascript:void(0)" onsubmit="search(this); return false;">
                    <tr>
                        <td>
                            <input type=text name=srchval value=""><input type=submit value="Search">
                        </td>
                    </tr>
                </form>
            </center>
        </body>
    </html>
    I've highlighted all the changes made along with some comments.

    Hope this helps.

  3. The Following User Says Thank You to codeexploiter For This Useful Post:

    KevT (09-02-2008)

  4. #3
    Join Date
    Sep 2008
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Thanks!

    SOLVED - and in record time!
    THANKS!

    I note the changes you made with much interest...

    ..rather than taking up your time asking for an explanation as to why IE behaves differently from FF for this (I'm sure there are others out there who could use your great help) - you got any sites/books you recommend to help me understand javascript better?

    Thanks thanks!

  5. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    I found the following mentioned items extremely useful

    Useful Links

    http://developer.mozilla.org/En/JavaScript
    http://msdn.microsoft.com/en-us/libr...50(VS.85).aspx
    http://www.howtocreate.co.uk/ - An excellent site with lots of useful information.

    Books

    JavaScript: The Definitive Guide by David Flanagan - The JavaScript Bible
    JavaScript: The Good Parts by Douglas Crockford - A rather inter to advanced book

    Hope this helps.

  6. #5
    Join Date
    Sep 2008
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Thanks again

    Thanks again - will keep those references in mind.

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
  •