HTML-escape javascript strings





12
Date Submitted Mon. Oct. 2nd, 2006 11:59 AM
Revision 1 of 1
Beginner drench
Tags escape | HTML | JavaScript | String
Comments 0 comments
Similar to Perl's CGI::escapeHTML(), though (because we can!) this adds it as a method to all String objects.


String.prototype.escapeHTML = function () {                                       
        return(                                                                 
            this.replace(/&/g,'&').                                         
                replace(/>/g,'>').                                           
                replace(/</g,'&lt;').                                           
                replace(/"/g,'&quot;')                                         
        );                                                                     
    };

// example

document.getElementById('some_div').innerHTML =
 document.getElementById('some_textarea').value.escapeHTML()

 

D R

dren.ch/

Comments

There are currently no comments for this snippet.

Voting