Do it server side, always the fastest - and most reliable.
You could do something like that :
$body = jQuery("body");
var words= ["blah", "blog", "blig"];
for(var i=0; i< words.size; i++){
$body.replace(words[i],words[i]+"&trade");
}
edit: using a "for" loop instead of foreach or .each will slightly increase the rendering time as explained in this google tech talk.
BUT I wouldn't do it and try a server side solution instead.
Browsing all the nodes to find a particular word is crazyly expensive in term of processing power. The loading time will also highly depend on the computer of the client.
You could also use CSS, though you'd need to wrap each occurrence of the name in an HTML tag:
.tm:after {
/* HTML entities don't work here but unicode entities like \u0123 do */
content: "™";
}
Lorem ipsum <span class="tm">The Company</span> etc etc
The advantage being you can easily change styles, colours etc.
Do a regex search/replace. In theory, this should be faster than marcgg's method since you are touching DOM only once.
var wordList = ["jQuery UI", "jQuery", "is"];
var regExp = new RegExp("\\b" + wordList.join("\\b|\\b") + "\\b", "g");
var $elem = $("#divWithText");
console.log(regExp);
$elem.html($elem.html().replace(regExp, "$&™"));
Caveat:
- This will also nuke any scripts that may be inside the div since the script elements are not re-parsed.
- Same applies to any html tag that matches your regex.
I don't know .NET but it must be possible to somehow do this on client side without having to tinker with the framework itself. A client side solution sounds really really bad to my ears.
In PHP, you can hold the output buffer and do operations on it before it is flushed. There must be something like that. A Regex or DOM operation that replaces all content outside tags (and, possibly, inside alt
attributes as well) should work quickly and cleanly.
In addition to what has already been written two more downsides to the JS approach come to mind:
- Product names get indexed in search engines without TMs.
- The
<title>
tag would have to be changed as well