Forum Moderators: not2easy

Message Too Old, No Replies

Highlighting text with CSS/javascript

Highlighting text with CSS/javascript single word only

         

mattboy

4:02 pm on Dec 4, 2003 (gmt 0)

10+ Year Member



On my website i have a search engine. when a user performs a search on my site i want the phrase "data sheet" to always appear highlighted if it is in the results page.

I've seen many ways that people have highlighed keywords relevant to the search performed, but all I need to do is highlight one word if it appears.

Does anyone know how to do this? with CSS? or javascript possibly?

to get an idea of the search on my site go to [extech.com...] and enter something like meter in the search box.

Thanks

DrDoc

11:40 pm on Dec 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World!

Are your pages static or dynamic?

mattboy

1:50 pm on Dec 5, 2003 (gmt 0)

10+ Year Member



My pages are static HTML

BlobFisk

1:53 pm on Dec 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How would users search your site? Using your own search facility? Or a Search Engine?

mattboy

2:02 pm on Dec 5, 2003 (gmt 0)

10+ Year Member



users search using the search box provided on my site. the saerch is indexed by atomz.com search. the results page is completely customized and i can include any piece of code I may need to on that results page.

My thinking is that there must be a way to use CSS or javascript to say that if a certain word appears on the page (in my case the word "datasheet") it would appear highlighted (in my case appear in orange bold)

I really appreciate all who are responding here. I've been posting this question on various forums with no luck.

THANKS

coopster

2:11 pm on Dec 5, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could put an onload attribute on the <body> element to run your javascript routine when the page is returned. In your javascript you could search for the keyword and place a style on it, such as

<span style="background:orange">[i]your_keyword_here[/i]</span>

mattboy

2:20 pm on Dec 5, 2003 (gmt 0)

10+ Year Member



I think i understand what you're getting at though i don't have any real expirence with writing javascript so if someone could come up with the code i need to place, I would really appreciate it.

In my research i thought maybe I could use some sort of a "document.getElementById" based upon the specific text "datasheet" Something like what appears here [webmasterworld.com...]

Problem is that I don't know anything about writing this kind of script.

Oh and my results page is dynamic, i know I said it was static before, but that's just the page with the search box on it.

so it's one of the dynamic words that turns up on the results page which I need to highlight, based on whether it appears in the results or not.

coopster

2:21 pm on Dec 5, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I just took a look at the link you mentioned in your first message of this thread. The result set is coming in like this:

<!-- ResultItemStart -->
some info here about the <span class="searchHighlight">meter</span> keyword
<!-- ResultItemEnd -->

Is this something Atomz puts in there for you? If so, all you have to do is add a class to your stylesheet namded
searchHighlight
and format it accordingly.

<edit reason>clarified which link was viewed</edit>

[edited by: coopster at 2:38 pm (utc) on Dec. 5, 2003]

mattboy

2:27 pm on Dec 5, 2003 (gmt 0)

10+ Year Member



I figured this might be difficult to explain.

Yes the "searchhighlight" thing is something Atomz puts in for me.

The thing is the even if someone does a search for "meter" on the site I want the results page to not only highlight the keyword which was searched upon but also anytime the word "datasheet" appears in the dynamic results page.

If there a was to write either some CSS or javascipt which would search that results page each time for the text "datasheet" and then highlight it in orange?

I wish I could be more clear. thanks for sticking with me to elp figure this out :)

coopster

2:51 pm on Dec 5, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Is than an option for you?
Highlighting Search Terms on Static Pages [webmasterworld.com]

BlobFisk

3:18 pm on Dec 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you can customise the results page, then coopsters link would seem to be the one for you (if it's not perl, all server side languages will do the same thing).

mattboy

3:36 pm on Dec 5, 2003 (gmt 0)

10+ Year Member



Coopster, that link to the highlight code is not really an option for me. I need to be able to do this with hopefully javascript.

I found this site which explains how to highlight words with javascript based on what search engine the page was refered from. [kryogenix.org...]

In my case though I don't need to based my results on where the user came from, but more on what text is on the page.

Does anyone know how to use the "getElementById" attribute? I was wondering if i could use this to say not by "Id" but by what text appears on the page.

mattboy

3:51 pm on Dec 5, 2003 (gmt 0)

10+ Year Member



I had a friend check out this disscussion and here was his response:

"You need to find that word as the text is written to the page - on the server side you would have to manipulate the results of the search query.

JavaScript is client side - meaning once text is part of an HTML page, there is very little, if anything, you can do to it. You could search for the word with JavaScript, but once you found it there would be nothing you could do to highlight it. Since you cant access the word with JavaScript, you cant really do anything with CSS for the same reason."

So now I understand a bit why I need to do this with Perl. Only problem is I don't know how to code Perl nor manipulate any Perl code. would anyone be able to help? I hate asking people to take the time to write this for me but unfortunately I don't have the knowledge to do it myself.

coopster

8:06 pm on Dec 5, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




I had a friend check out this disscussion and here was his response:

"You need to find that word as the text is written to the page - on the server side you would have to manipulate the results of the search query.

JavaScript is client side - meaning once text is part of an HTML page, there is very little, if anything, you can do to it. You could search for the word with JavaScript, but once you found it there would be nothing you could do to highlight it. Since you cant access the word with JavaScript, you cant really do anything with CSS for the same reason."


I beg to differ:


highlighting text with CSS/javascript

<html><head><title>Datasheet</title>
<!-- First off, let's give this fella his propers:
[kryogenix.org...] -->
<script type="text/javascript">
function highlightWord(node,word) {
if (node.hasChildNodes) {
var hi_cn;
for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
highlightWord(node.childNodes[hi_cn],word);
}
}
if (node.nodeType == 3) {
tempNodeVal = node.nodeValue.toLowerCase();
tempWordVal = word.toLowerCase();
if (tempNodeVal.indexOf(tempWordVal)!= -1) {
pn = node.parentNode;
if (pn.className!= "searchword") {
nv = node.nodeValue;
ni = tempNodeVal.indexOf(tempWordVal);
before = document.createTextNode(nv.substr(0,ni));
docWordVal = nv.substr(ni,word.length);
after = document.createTextNode(nv.substr(ni+word.length));
hiwordtext = document.createTextNode(docWordVal);
hiword = document.createElement("span");
hiword.className = "searchword";
hiword.appendChild(hiwordtext);
pn.insertBefore(before,node);
pn.insertBefore(hiword,node);
pn.insertBefore(after,node);
pn.removeChild(node);
}
}
}
}
function googleSearchHighlight() {
if (!document.createElement) return;
ref = document.referrer;
highlightWord(document.getElementsByTagName("body")[0],'datasheet');
}
window.onload = googleSearchHighlight;
</script>
<style type="text/css">
span.searchword {
font-weight: bold;
color: #FF9900;
}
</style></head><body><p>
<!-- ResultItemStart -->
Here is the text that is returned to you!<br />
Thank you for visiting our page and searching for meters.<br />
There is a great datasheet about meters you may want to check out as well!<br />
<!-- ResultItemEnd -->
</p></body></html>

I used the code in the link you provided (modified his js to meet your particular needs).

mattboy

8:33 pm on Dec 5, 2003 (gmt 0)

10+ Year Member



WELL COOPSTER I guess we all know who the genius is now!

Thank you so so much for figuring that out and posting an example of the working code!

I don't know how to repay you but offer my thanks

One thig is that i wish it worked in Netscape 4.7 on the PC. Oh well can't ask for it all!

coopster

9:21 pm on Dec 5, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You're welcome. I've done text highlighting before on form elements, this guy did his on the body tag. It's a nice, well-written piece of work. I knew it could be done but one of the things I like about this forum though is that nobody likes to flat out do your homework for you -- you are really encouraged to dig in and learn. If you are going to be doing any development, I recommend tearing into a sample piece of code like this and learning how things work. Just some friendly encouragement, mattboy.

This code is only going to work in user agents that support it, and that means they have to have javascript enabled in their browser settings. I've developed my share of js, some of it fairly advanced, but all of it short of genius ;) Thanks for the compliment though.

I'm more drawn to server-side development and solutions :)