Help - Search - Members - Calendar
Full Version: My javascript search engine doesn't work in Firefox
MSFN Forums > Coding, Scripting and Servers > Web Development (HTML, Java, PHP, ASP, XML, etc.)

   
Google Internet Forums Unattended CD/DVD Guide
Shoshoni
OK I got this nice little script from a website somewhere. What it does, you enter a text string, press enter and then highlights the text. Very simple. I've searched high and low for how to get it to work in Firefox (I found some things but to no avail). From what I understand this script only supports ie4 and up and netscape.
Here's the script. If someone has a solution I would be very grateful. Thanks in advance! welcome.gif

CODE
<script language="JavaScript">
<!-- More javascripts http://www.hypergurl.com -->

var NS4 = (document.layers);    // Which browser?
var IE4 = (document.all);


var win = window;    // window to search.
var n   = 0;

function findInPage(str) {

  var txt, i, found;

  if (str == "")
    return false;

  // Find next occurance of the given string on the page, wrap around to the
  // start of the page if necessary.

  if (NS4) {

    // Look for match starting at the current point. If not found, rewind
    // back to the first match.

    if (!win.find(str))
      while(win.find(str, false, true))
        n++;
    else
      n++;

    // If not found in either direction, give message.

    if (n == 0)
      alert("Not found.");
  }

  if (IE4) {
    txt = win.document.body.createTextRange();

    // Find the nth match from the top of the page.

    for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
      txt.moveStart("character", 1);
      txt.moveEnd("textedit");
    }

    // If found, mark it and scroll it into view.

    if (found) {
      txt.moveStart("character", -1);
      txt.findText(str);
      txt.select();
      txt.scrollIntoView();
      n++;
    }

    // Otherwise, start over at the top of the page and find first match.

    else {
      if (n > 0) {
        n = 0;
        findInPage(str);
      }

      // Not found anywhere, give message.

      else
        alert("Not found.");
    }
  }

  return false;
}

</script>
Shoshoni
no one ? really ??
Chozo4
So your basically trying to recreate firefox's search method already?

Also, did you also open firefoxes 'java console' (found in tools?) to see if any javascript errors had been reported back?
Shoshoni
I'll look into it thanks
Shoshoni
It doesn't report anything to me.
Shoshoni
I noticed that the search script on the site i got it from http://www.hypergurl.com/searchpage.html doesn't work as well so that's one step ahead. Anyone ideas of how to make it FF compatible?
billybob bo buck
didn't Chozo4 already tell you that firefox already has this feature, i mean what's the point?
Shoshoni
i'm such an (..) i was thinking about it this afternoon. still it bugs me that it doesn't work but ah well.
Tomcat76
That's a really old script. The JavaScript sniffer stems from the Netscape 4 days...

I'm not sure what the script is supposed to be doing but here's a version that should theoretically do it...

CODE
<script type="text/javacript">

var win = window;    // window to search.
var n   = 0;

function findInPage(str) {

  var txt, i, found;

  if (str == "")
    return false;

  // Find next occurance of the given string on the page, wrap around to the
  // start of the page if necessary.


  if (window.execScript) {
    txt = win.document.body.createTextRange();

    // Find the nth match from the top of the page.

    for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
      txt.moveStart("character", 1);
      txt.moveEnd("textedit");
    }

    // If found, mark it and scroll it into view.

    if (found) {
      txt.moveStart("character", -1);
      txt.findText(str);
      txt.select();
      txt.scrollIntoView();
      n++;
    }

    // Otherwise, start over at the top of the page and find first match.

    else {
      if (n > 0) {
        n = 0;
        findInPage(str);
      }

      // Not found anywhere, give message.

      else
        alert("Not found.");
    }
  } else {

    // Look for match starting at the current point. If not found, rewind
    // back to the first match.

    if (!win.find(str))
      while(win.find(str, false, true))
        n++;
    else
      n++;

    // If not found in either direction, give message.

    if (n == 0)
      alert("Not found.");
  }
  return false;
}

</script>


@Chozo4... Java and JavaScript are two different things.
javeflorent
Hi,
@Tomcat76: really good it works with every internet browsers.
Is there a way to perform such s "string search" in an iframe? The iFrame is located in the main page.
So just a simple page with an iframe in it an the research is performed on the iframe content...
I tried changing the window to search like this:
---
var win = window;
win=parent.frames[myframe];
---
or
---
var win = window;
win=document.getElementById('myframe').contentWindow;
---
I tried both but it does not work.

Any other ideas ?
Google Internet Forums Unattended CD/DVD Guide
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.