Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for a word within the current HTML document

Status
Not open for further replies.

Ikramy

Programmer
Jul 10, 2001
15
AE
How can I do a simple search-engine that looks for a word inside the current HTML document? I mean, if the search form is inside the page default.htm then the search-engine will look for the word inside default.htm.
 
With Javascript!
Code:
<html>
<head>
<script>
<!-- 
var NS4 = (document.layers);
var IE4 = (document.all);
var win = this;
var n   = 0;
function findInPage(str) {
var txt, i, found;
if (str == &quot;&quot;)
return false;
if (NS4) {
if (!win.find(str))
while(win.find(str, false, true))
n++;
else
n++;
if (n == 0) alert(str + &quot; was not found on this page.&quot;);
}
if (IE4) {
txt = win.document.body.createTextRange();
for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
txt.moveStart(&quot;character&quot;, 1);
txt.moveEnd(&quot;textedit&quot;);
}
if (found) {
txt.moveStart(&quot;character&quot;, -1);
txt.findText(str);
txt.select();
txt.scrollIntoView();
n++;
}
else {
if (n > 0) {
n = 0;
findInPage(str);
}
else
alert(str + &quot; was not found on this page.&quot;);
}
}
return false;
}
// -->
</script>
</HEAD>

<BODY>
<form name=search onSubmit=&quot;return findInPage(this.string.value);&quot;>
Find in Page
<input name=string type=text size=15 onChange=&quot;n = 0;&quot;>
</form>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top