simple question

i am a newbie

Can anyone suggest how i can search for a specific occurance of a word on a webpage, and construct a highlight button so that when i click it "on " i can highlight all the occurances and when i click it "off" i can view the html page without the highlighting

VlearnsAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
COBOLdinosaurConnect With a Mentor Commented:
This will work for all modern browsers.  Tested on IE6 and FF1

I post some documentation in a post following this one.

Cd&



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
   <title> Search and Highlight </title>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <script type="text/javascript">
   save=false;
   function setBlue(param,src)
   {
      str= new String(param);
      arg= new RegExp(param,'gi');
      src.value='';
      x=new String(document.body.innerHTML);
      save=x;
      x=x.replace(arg,str.fontcolor('blue'));
      document.body.innerHTML=x;
   }
   function clearBlue()
   {
      if(save) document.body.innerHTML=save;
   }
  </script>
</head>
<body>
<form>
Search For:<input type="text" name="sArg">
<input type="button" value="Highlight" onclick="setBlue(this.form.sArg.value,this.form.sArg)">
<input type="button" value="Clear" onclick="clearBlue()">
</form>
<p>
Lorem ipsum dolor sit amet, lacus nonummy tempus, dui nascetur. Sociosqu luctus habitasse. Tempus dictumst curabitur sit id malesuada ipsum, at facilisi dignissim. Hendrerit potenti. Convallis nam. Cursus inceptos risus pretium nec aliquam nascetur. Ultricies vivamus eros congue conubia bibendum magnis nullam malesuada nam ultrices placerat iaculis.
Blandit eu nunc egestas sociosqu cubilia sagittis a mollis nascetur. Nisl euismod curae vivamus. Eleifend fermentum erat. Tempus semper hymenaeos scelerisque massa, purus cursus mus sociis libero. Eu fermentum sodales. Integer ac vestibulum wisi facilisis consequat, vitae mattis enim donec. Molestie orci.</p>
<p>
A dictumst sollicitudin quam habitasse integer netus scelerisque neque orci. Luctus hendrerit quam tempus iaculis praesent tempor per, sociosqu sollicitudin dictum bibendum. Nisl neque ornare viverra ultricies parturient cursus rhoncus inceptos mauris cursus vivamus faucibus. In orci class quis. Conubia condimentum sollicitudin scelerisque varius platea vivamus adipiscing, purus viverra. Nullam nisl elit est quis. Et, diam orci neque ultrices erat.
Habitant pretium. Suscipit ante. Volutpat suscipit odio. Nostra nunc ultrices eleifend egestas cursus. Diam cursus enim habitasse sed, sociis et. Sit interdum mi ridiculus pretium id, senectus tincidunt aenean sem pulvinar pellentesque. Et libero. </p>
<p>
Lorem ipsum dolor sit amet, lacus nonummy tempus, dui nascetur. Sociosqu luctus habitasse. Tempus dictumst curabitur sit id malesuada ipsum, at facilisi dignissim. Hendrerit potenti. Convallis nam. Cursus inceptos risus pretium nec aliquam nascetur. Ultricies vivamus eros congue conubia bibendum magnis nullam malesuada nam ultrices placerat iaculis.
Blandit eu nunc egestas sociosqu cubilia sagittis a mollis nascetur. Nisl euismod curae vivamus. Eleifend fermentum erat. Tempus semper hymenaeos scelerisque massa, purus cursus mus sociis libero. Eu fermentum sodales. Integer ac vestibulum wisi facilisis consequat, vitae mattis enim donec. Molestie orci.</p>
<p>
A dictumst sollicitudin quam habitasse integer netus scelerisque neque orci. Luctus hendrerit quam tempus iaculis praesent tempor per, sociosqu sollicitudin dictum bibendum. Nisl neque ornare viverra ultricies parturient cursus rhoncus inceptos mauris cursus vivamus faucibus. In orci class quis. Conubia condimentum sollicitudin scelerisque varius platea vivamus adipiscing, purus viverra. Nullam nisl elit est quis. Et, diam orci neque ultrices erat.
Habitant pretium. Suscipit ante. Volutpat suscipit odio. Nostra nunc ultrices eleifend egestas cursus. Diam cursus enim habitasse sed, sociis et. Sit interdum mi ridiculus pretium id, senectus tincidunt aenean sem pulvinar pellentesque. Et libero. </p>
<p>
Lorem ipsum dolor sit amet, lacus nonummy tempus, dui nascetur. Sociosqu luctus habitasse. Tempus dictumst curabitur sit id malesuada ipsum, at facilisi dignissim. Hendrerit potenti. Convallis nam. Cursus inceptos risus pretium nec aliquam nascetur. Ultricies vivamus eros congue conubia bibendum magnis nullam malesuada nam ultrices placerat iaculis.
Blandit eu nunc egestas sociosqu cubilia sagittis a mollis nascetur. Nisl euismod curae vivamus. Eleifend fermentum erat. Tempus semper hymenaeos scelerisque massa, purus cursus mus sociis libero. Eu fermentum sodales. Integer ac vestibulum wisi facilisis consequat, vitae mattis enim donec. Molestie orci.</p>
<p>
A dictumst sollicitudin quam habitasse integer netus scelerisque neque orci. Luctus hendrerit quam tempus iaculis praesent tempor per, sociosqu sollicitudin dictum bibendum. Nisl neque ornare viverra ultricies parturient cursus rhoncus inceptos mauris cursus vivamus faucibus. In orci class quis. Conubia condimentum sollicitudin scelerisque varius platea vivamus adipiscing, purus viverra. Nullam nisl elit est quis. Et, diam orci neque ultrices erat.
Habitant pretium. Suscipit ante. Volutpat suscipit odio. Nostra nunc ultrices eleifend egestas cursus. Diam cursus enim habitasse sed, sociis et. Sit interdum mi ridiculus pretium id, senectus tincidunt aenean sem pulvinar pellentesque. Et libero. </p>
</body>
</html>

0
 
percynCommented:
What browsers do you want this to be compatible with?
Are the use of 3rd party components acceptable?
0
 
VlearnsAuthor Commented:
IE only
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
DaydreamsCommented:
Hi Vlearns,

If you can't find what you want with IE, you may want to try the Firefox browser:

http://www.mozilla.org/products/firefox/

..and add the functionality of this tool (Advanced Highlighter Button 1.51):

http://update.mozilla.org/extensions/showlist.php?category=Search%20Tools

..which is described here (with screenshots as well):

http://membres.lycos.fr/pcdingo/  (scroll down to Advanced Highlighter Button)

0
 
DaydreamsCommented:
That toolbar has a lots of funcionality, but the Firefox browser's latest version has a "find toolbar" that will highlight as well.

http://www.mozilla.org/products/firefox/releases/

From that page:

"Find is easier and more powerful now with our new Find toolbar. The Find toolbar (which shows at the bottom of the browser window) automatically highlights text in the page as you type and has a useful highlight feature."

I know you want this ability for IE, but I thought I'd mention an alternative.
0
 
VlearnsAuthor Commented:
yes that would be helpful, thanks for that ..
but i need to know how this would be implemented.
Traversing the DOM to search for specific occurance and surround that instance by a stylesheet?

does anyone know how to achieve this programmatically?

0
 
OliWarnerCommented:
>> Traversing the DOM to search for specific occurance and surround that instance by a stylesheet?
yes, exactly that!

Just replaces the "word" with <span id="highlight">word</span>


cached google pages do this server side but there is no reason why you couldnt do this client side with javascript.
0
 
OliWarnerCommented:
that span assumes you have a #highlight selector defined in some CSS, CSS which can just be added to the page...
0
 
COBOLdinosaurCommented:
Formatting options:
str.fontcolor('blue') can be replace by any of these string methods:
str.big()
str.blink()
str.bold()
str.fixed()
str.fontsize('5')
str.small()
str.strike()
str.sub()
str.sup()

Basically the properties of the font tag.  The whole thing is accompliched by Javascript inserting a font tag inline.

If you want the search to be case sensitive change the "gi" in the regular expression with "g".

If the search argument is a tag name, property attribute or a literal value found inside the tag, the page formatting may get screwed up, and some of the HTML may be displayed.  The only way around that would be with very complex filtering in the script, or processing individual text nodes instead of the body node itself.  That would be a substantial amount fo work and I'm not looking for a project.  You would probably have to hire someone if you want something that detailed and robust.

Cd&
0
 
VlearnsAuthor Commented:
do you  have that documentation in english?
0
 
COBOLdinosaurCommented:
What don't you understand.  The setting of the highlight of the Regular Expression?

Cd&
0
 
VlearnsAuthor Commented:
nope its fine, was a bit confused before, i understand it now.


thanks

0
 
VlearnsAuthor Commented:
that answers my question
0
 
COBOLdinosaurCommented:
Glad we could help.  Thanks for the A. :^)

Cd&
0
All Courses

From novice to tech pro — start learning today.