Robert Granlund
asked on
Search a Page for Content
Is there a way to place a searchbox on my page, that when I type into it it will start to find matches? Like the way if you use cntl+F in Firefox and you type a word in and it highlights on the page?
Can you explain the need a little more? For example, why wouldn't the client just use ctrl+F, since it is already built in?
ASKER
The client does not use FF. Also they are very "UN-Technical" . I want to as ease to the page that contains a bunch of information.
I wrote this ages ago, it might work for you. Just cut and paste into the top of the page.
edit: (Just tested on IE and it works, did not on FF)
edit: (Just tested on IE and it works, did not on FF)
<script language="JavaScript">
//Script by GwynforWeb, enjoy and use freely.
// Set browser variables
var IE4 = (document.all);
var NS4 = (document.layers);
// Set browser window for searching
var win = window;
var n = 0;
function FindString(str)
{
var PageText, Found, i;
if (str == "")
return false;
// Sniff for browser
// Look for match starting at the current point winding around to first match
if (NS4)
{
if (!win.find(str))
while(win.find(str, false, true))
n++;
else
n++;
// If not send message.
if (n == 0)
alert(" Not Found on Page.");
}
if (IE4)
{
// Set search object to whole page
PageText = win.document.body.createTextRange();
// Search for nth match from top.
for (i = 0; i <= n && (Found = PageText.findText(str)) != false; i++)
{
PageText.moveEnd("textedit");
PageText.moveStart("character", 1);
}
// Scroll to and mark text if Found.
if (Found)
{
PageText.moveStart("character", -1);
PageText.findText(str);
PageText.scrollIntoView();
PageText.select();
n++;
}
// Else, goto top of the page and find first match.
else
{
if (n > 0)
{
n = 0;
FindString(str);
}
// Not Found then send message.
else
alert("Not Found on Page");
}
}
return false;
}
</script>
<input onchange="n = 0;" size="17" name="SearchString"></p>
<input type="submit" value="Find on Page"> </p>
The client does not use FF.Tested ctrl-F on FF, IE, and Chrome. In all of those browsers it opened a "find" dialog box that not only worked, it featured autocomplete. On Safari, it was apple-F.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.javascripter.net/faq/searchin.htm