Need an efficient way to replace html entities.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mike_walters
Posted On:   Thursday, February 28, 2008 11:37 AM

I need a way in JavaScript to replace all html entities with their code value. I have a script that works, but it must loop through every character so it is no good for large files. Here is what I have now and want to improve: function coded(input) { var output = new String(); for( i = 0; i < input.length; i++) { var myChar = input.charAt(i); if (myChar.match(/[ws !@#$\%^&*()-\_=+;:\'"?., <>/\{}[]]/)) { output += myChar; } else { output += '&#' + mychar.charcodeat(0) + ';'; } } return output; }    More>>

I need a way in JavaScript to replace all html entities with their code value. I have a script that works, but it must loop through every character so it is no good for large files. Here is what I have now and want to improve:

			
function coded(input) {

var output = new String();

for( i = 0; i < input.length; i++) {
var myChar = input.charAt(i);
if (myChar.match(/[ws
!@#$\%^&*()-\_=+;:\'"?., <>/\{}[]]/)) {
output += myChar;
}
else {
output += '&#' + mychar.charcodeat(0) + ';';
}
}
return output;
}
   <<Less

Re: Need an efficient way to replace html entities.

Posted By:   Firat_Tiryaki  
Posted On:   Friday, February 29, 2008 01:47 AM

I am not sure what you want to do but the "innerHTML" property of the DOM elements works for you. For example -> document.body.innerHTML
About | Sitemap | Contact