/    Sign up×
Community /Pin to ProfileBookmark

One script to make certain words bold

Good day to you, everybody!

I want to make an html document smaller by putting a code on top of the page, that would change defined words in the rest of the document to bold. I don’t know, if it’s possible, though…

So, instead of placing bold tag next to one an the same word all over the document:

<b>rabbit</b>

I want to have a script probably in the head section, that would find the defined words (like “rabbit” in my example) and make ’em bold. The point is not to have any tags next to the rabbit!

Can it be done?

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@bathurst_guySep 09.2005 — ahhh, bit complicated i reckon, does this need to be done on the fly, like displaying search words within search results, or static. If static i just recommend a good ol find and replace. but otherwise i wouldnt know where to start
Copy linkTweet thisAlerts:
@BigMoosieSep 09.2005 — Perhaps something like this:

&lt;SCRIPT type="text/javascript"&gt;
function moose(container){
var e, i = 0;
while (e = container.childNodes[i++]) {
if (e.nodeName == '#text') container.innerHTML = container.innerHTML.split("rabbit").join("&lt;B&gt;rabbit&lt;/B&gt;");
if (e.hasChildNodes()) moose(e);
}
}
window.onload = function(){
moose(document.getElementsByTagName('body')[0]);
}

&lt;/SCRIPT&gt;
Copy linkTweet thisAlerts:
@herodote92Sep 09.2005 — Interesting script, seems to work ok, but there must be a small problem somewhere. I included in my html :
<i>
</i>&lt;span&gt;rabbit&lt;ul&gt;&lt;li&gt;big rabbit 2&lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;
&lt;u&gt;little rabbit 3&lt;/u&gt;
&lt;p&gt;rabbit 4&lt;/p&gt;


Then I added an alert at the end of the window.onload() function to check the innerHTML, and I got something like:
<i>
</i>&lt;span&gt;&lt;b&gt;&lt;b&gt;&lt;b&gt;&lt;b&gt;&lt;b&gt;rabbit&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;ul&gt;&lt;li&gt;big&lt;b&gt;&lt;b&gt;&lt;b&gt;&lt;b&gt;&lt;b&gt;&lt;b&gt;&lt;b&gt;rabbit&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/b&gt; 2&lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;&lt;u&gt;little &lt;b&gt;&lt;b&gt;&lt;b&gt;&lt;b&gt;&lt;b&gt;&lt;b&gt;&lt;b&gt;rabbit&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/b&gt; 3&lt;/u&gt;&lt;p&gt;&lt;b&gt;&lt;b&gt;&lt;b&gt;&lt;b&gt;&lt;b&gt;&lt;b&gt;rabbit&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/b&gt; 4&lt;/p&gt;


This makes those rabbits [COLOR=Red][B]very fat[/B] [/COLOR], as it seems :eek: .

I think it's because once you have included a <b> tag, you have created a new child for the current node, but I'm not sure of how to correct it at the moment.
Copy linkTweet thisAlerts:
@BiontauthorSep 10.2005 — Mr.Moose, thank you very much. It's a neat script, eventhough it's really slow. I tried to use it on HTML document, that weights 1 Mb and has at least a thousand of those "rabbits". On my Sempron 2300 it takes 1 minute to load.
Copy linkTweet thisAlerts:
@CharlesSep 10.2005 — Is it possible for you to put every separate word in its own SPAN element?
Copy linkTweet thisAlerts:
@BiontauthorSep 10.2005 — If I understand you right... that's a crazy idea. Instead of <b>rabbit</b> I would then have <span blah-blah-blah>rabbit</span>. 8I
Copy linkTweet thisAlerts:
@CharlesSep 10.2005 — Yes, but you could have:&lt;p&gt;
&lt;span&gt;Fee&lt;/span&gt;,
&lt;span&gt;Fie&lt;/span&gt;,
&lt;span&gt;Foe&lt;/span&gt;,
&lt;span&gt;Fum&lt;/span&gt;.
&lt;/p&gt;
and then decide dynamically which one to emphasize.
Copy linkTweet thisAlerts:
@BiontauthorSep 10.2005 — Sorry, I don't quite catch you. Are you saying, that if I have 500 Fee's, 800 Fie's, 1200 Foe's and 2500 Fum's, I got to have some kind of <span> tag around each one of them? Or am I missing your point?

My mission is to free HTML of redundant tags. Moose's script is perfect. It's a small code that replaces certain words in the whole document. The only minus is that it's working slow. But I found, that the amount of code I save is not worth using that script. HTMLZip is a much better solution.

But still, that script by Moose can be used for other purposes.

So, what is your idea?
Copy linkTweet thisAlerts:
@CharlesSep 11.2005 — So, what is your idea?[/QUOTE]I'm trying to figure out just what it is that you are trying to accomplish. If you want to highlight words dynamically then the easy way is to put each word in its own SPAN element.
Copy linkTweet thisAlerts:
@BigMoosieSep 11.2005 — This is better, I was over complicating things unncessarily, just be sure not to boldify any words that are likely to be used inside your attributes:

function bolidfy(container, str){
container.innerHTML = container.innerHTML.split(str).join('&lt;B&gt;'+str+'&lt;/B&gt;');
}
window.onload = function(){
bolidfy(document.getElementsByTagName('body')[0], 'rabbit');
}
×

Success!

Help @Biont 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.20,
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,
)...