+3  A: 

Do it server side, always the fastest - and most reliable.

Marcus
+1 If it's important for you to show that a some text is a registered trademark for legal reasons, then you'll want to be sure people don't just disable javascript and avoid the trademark notification. Also a javascript error might prevent the trademarking from showing up in some browsers.
blesh
I understand that server side is the best way. Server side is impossible here.
Pselus
+4  A: 

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.

marcgg
This doesn't work. I get no changes. to my page when I replaced your word list with mine.
Pselus
You may want to account for spaces/sentence structure around the words. Otherwise you can wind up like many low-end profanity checkers that don't account for words like "class".
Agent_9191
You have to tweak the code a bit, I haven't tested it. Check for JS errors.
marcgg
-1: Egads! Don't (especially incorrectly) attempt to do a text/innerHTML find/replace on the `<body>` element, you can break... everything.
eyelidlessness
@eyelidlessness: my point was mainly NOT to do that
marcgg
A: 

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.

DisgruntledGoat
this doesn't work cross browser...
marcgg
True, but I thought I'd throw it out there. "Add TM to text with jQuery" is pretty arbitrary in the first place.
DisgruntledGoat
+1  A: 

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, "$&&trade;"));

Caveat:

  1. This will also nuke any scripts that may be inside the div since the script elements are not re-parsed.
  2. Same applies to any html tag that matches your regex.
Chetan Sastry
How can I apply an array to this?Do I need to loop through my array and touch the DOM for each word in the list?
Pselus
And could you explain this a little more for those of us that don't know Regex at all?Is this looking for the words "jQuery UI", "jQuery", and "is" or is it looking for "jQuery UI jQuery is"?
Pselus
It looks for either :jQuery", "jQuery UI" or "is". You can build a regex dynamically with a list.
Chetan Sastry
Ok, this method almost worked.Ironically, when it finds an <a> with the word in the address, it adds "TMTM" to that, but does NOT add "TMTM" to the actual word that the link surrounds.Also, it is adding "TMTM", not "TM".
Pselus
It is getting there, but it still has the issue where, with a link it replaces the word in the href, but not in the linked text.So for example: "a href='page.html'>page</a" ends up with "a href='page™.html'>page</a"
Pselus
Agreed, the regex sucks. It basically looks for a "whole word" matches only and doesn't seem to match >`page`<. This problem is harder than it seems.
Chetan Sastry
Why does it find "page" in the address then if the address is like "/0/products/page"?
Pselus
moved the question here using your solution:http://stackoverflow.com/questions/1841911/regex-to-find-word-on-page-including-inside-tags
Pselus
I think you can fix some of the regex problems by using a character class to find [^\s>] before the word and [\b$<] after (or something similar, my regex isn't that good).
Joel Potter
A: 

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
Pekka