Asked 8 years ago
3 Oct 2016
Views 996
kord

kord posted

how to escape html in javascript

Hi

i need a function function where we can pass the html string and it give us output escaped html in javascript

Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00


if you want to add framework to escape html only than use below function to avoid extra load


function html_escape (str) {
    if (!str) {
      return str;
    }
	 
    return str.replace(/&/g, '&')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;');
}
jagdish

jagdish
answered Nov 30 '-1 00:00

1. Prototype Js
Prototype js have escapeHTML function to escape html


var d='<div id="test23">This is simple test</div>'
d.escapeHTML();// prototype js escape html


this code escape Html of varable d

2. YUI framework
Prototype js have html function to escape html

var d='<div id="test23">This is simple test</div>'
d.html();// YUI framework escape html
Post Answer