Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add TM to text with jQuery?

I need a way to go through all the text on my page, including links and other controls and find words that are in a certain list and add the html character entity ™ (™) to them. I need this to be fast too. The list is held in a javascript array. I've already got code using .each to find all Links on the page with text from that list, but it's noticeably slow and I don't like that.

Any better, more efficient ways to do this?

EDIT

People are suggesting other alternatives (server-side, css, etc.) We can't use those because these words are in URL's all over the site. We would mess up our URL's all over the site. We are using DotNetNuke to do this and the client just told us today that every time their products appear on the entire site (including links) they want them to be in all caps and have TM appended. If we change the products in the database, all the links suddenly have "trade" appended to the end of them. The nature of DNN says that we can't do this server-side. We could go through and manually change it in each page...but the site is 1,900+ pages...... SO! Client side is the route we want to go.

like image 537
James P. Wright Avatar asked Dec 18 '25 23:12

James P. Wright


2 Answers

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.

like image 129
marcgg Avatar answered Dec 20 '25 13:12

marcgg


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

like image 28
LJW Avatar answered Dec 20 '25 13:12

LJW



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!