Jump to content

My javascript search engine doesn't work in Firefox


Recommended Posts

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! :hello:

<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>

Link to comment
Share on other sites


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?

Link to comment
Share on other sites

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...

<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.

Link to comment
Share on other sites

  • 1 year later...

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 ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...