/    Sign up×
Community /Pin to ProfileBookmark

help – find / search on page

Greetings!

Can anyone help me modify my script? I was trying to produce a simple-cross browser search option that would search / highlight the text on just one page – much like the ctrl+f function.

Currently a vistor would need to click “search page” over and over again to highlight each instance of the same word, one at a time. I would like just one click and all examples of that term to be highlighted. For instance, all the examples of “child” would be highlighted at once.

The test page is here:

[url]http://thecenterforcounselingarts.com/index_testsearch.html[/url]

The script I’m using is below.

Very best and many thanks,

R

script:

<script language=”JavaScript”>
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.find) {
// Look for match starting at the current point. If not found, rewind
// back to the first match.
if (!window.find(str)) {
while (window.find(str, false, true)) {
n++;
}
} else {
n++;
}
// If not found in either direction, give message.
if (n == 0) {
alert(“Not found.”);
}
} else if (window.document.body.createTextRange) {
txt = window.document.body.createTextRange();
// Find the nth match from the top of the page.
found = true;
i = 0;
while (found === true && i <= n) {
found = txt.findText(str);
if (found) {
txt.moveStart(“character”, 1);
txt.moveEnd(“textedit”);
}
i += 1;
}
// If found, mark it and scroll it into view.
if (found) {
txt.moveStart(“character”, -1);
txt.findText(str);
txt.select();
txt.scrollIntoView();
n++;
} else {
// Otherwise, start over at the top of the page and find first match.
if (n > 0) {
n = 0;
findInPage(str);
}
// Not found anywhere, give message. else
alert(“Not found.”);
}
}
return false;
}

</script>

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @thedistancert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...