×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Add Control + F feature to a Web Page

Add Control + F feature to a Web Page

Add Control + F feature to a Web Page

(OP)
Hello All

I am using the following script to "find text on a web page" and although it highlights the first instance of the text, the page defaults to the top of the page.

Does anyone know how to fix this?

When the text is highlighted, I would like the browser to go to that portion of the web page.

Thank you in advance for any assistance you can provide.




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiona...">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
.highlighted
{
background-color: yellow;
}
.highlight
{
background-color: #fff34d;
-moz-border-radius: 5px; /* FF1+ */
-webkit-border-radius: 5px; /* Saf3-4 */
border-radius: 5px; /* Opera 10.5, IE 9, Saf5, Chrome */
-moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.7); /* FF3.5+ */
-webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.7); /* Saf3.0+, Chrome */
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.7); /* Opera 10.5+, IE 9.0 */
}
.highlight
{
padding: 1px 4px;
margin: 0 -4px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11...." type="text/javascript"></script>
<script type="text/javascript">
function searchAndHighlight(searchTerm, selector) {
if (searchTerm) {
var selector = selector || "#realTimeContents"; //use body as selector if none provided
var searchTermRegEx = new RegExp(searchTerm, "ig");
var matches = $(selector).text().match(searchTermRegEx);
if (matches != null && matches.length > 0) {
$('.highlighted').removeClass('highlighted'); //Remove old search highlights

//Remove the previous matches
$span = $('#realTimeContents span');
$span.replaceWith($span.html());

if (searchTerm === "&") {
searchTerm = "&amp;";
searchTermRegEx = new RegExp(searchTerm, "ig");
}
$(selector).html($(selector).html().replace(searchTermRegEx, "<span class='match'>" + searchTerm + "</span>"));
$('.match:first').addClass('highlighted');

var i = 0;

$('.next_h').off('click').on('click', function () {
i++;

if (i >= $('.match').length) i = 0;

$('.match').removeClass('highlighted');
$('.match').eq(i).addClass('highlighted');
$('.ui-mobile-viewport').animate({
scrollTop: $('.match').eq(i).offset().top
}, 300);
});
$('.previous_h').off('click').on('click', function () {

i--;

if (i < 0) i = $('.match').length - 1;

$('.match').removeClass('highlighted');
$('.match').eq(i).addClass('highlighted');
$('.ui-mobile-viewport').animate({
scrollTop: $('.match').eq(i).offset().top
}, 300);
});




if ($('.highlighted:first').length) { //if match found, scroll to where the first one appears
$(window).scrollTop($('.highlighted:first').position().top);
}
return true;
}
}
return false;
}

$(document).on('click', '.searchButtonClickText_h', function (event) {

$(".highlighted").removeClass("highlighted").removeClass("match");
if (!searchAndHighlight($('.textSearchvalue_h').val())) {
alert("No results found");
}


});
</script>
</head>
<body>
<div class="searchContend_h">
<div class="ui-grid-c">
<div class="ui-block-a">
<input name="text-12" id="text-12" value="" type="text" class="textSearchvalue_h" />
</div>
<div class="ui-block-b">

<a href="#" class="searchButtonClickText_h">Search |</a>




<a href="#" class="next_h">Next |</a>


<a href="#" class="previous_h">Previous</a>
</div>
<div id="realTimeContents">



<p>Testing



<p>A is For Apple.

<p>B is for Boy.

<p>C is for Car.

<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>


<p>C is for Car.


</div>
</div>
</div>

RE: Add Control + F feature to a Web Page

Hi

You call the .animate() on an element with class ui-mobile-viewport, but no such element exists in the code you posted.
Better call it on both html and body :

CODE --> JavaScript ( fragment )

$('html, body').animate({
    scrollTop: $('.match').eq(i).offset().top
}, 300); 
( Sorry, can not provide explanation on that selector. sad )

I noticed it does not work reliably, for now blaming the incomplete document. My test indicates that it should work.

Feherke.
feherke.github.io

RE: Add Control + F feature to a Web Page

(OP)
Thank you.

I updated the post.

Does it work for you now?

I still can not get it to work correctly though.

Thank you.

RE: Add Control + F feature to a Web Page

Hi

I can not spot out any change in your code. Neither the change I suggested nor other. So it does not work and that was expectable.

Feherke.
feherke.github.io

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close