Strip HTML Tags from String
This Javascript function removes all HTML tags from a string. If a user submits comments or other content, HTML tags need to be stripped in order control formatting. This function removes any potentially harmful code or user formatted code so your input is clean.
function strip_tags(i) {
var tmp = document.createElement("DIV");
tmp.innerHTML = i;
return tmp.textContent||tmp.innerText;
}
Example:
textcomment = "Hello, my name is <b onclick='alert(\"hi\")>George</b> and I live in the jungle. <a href='monkey.html'>I have a pet monkey.</a> They call me George of the Jungle.";
textcomment = strip_tags(textcomment);
Produces the result:
The string below no longer has the <b onclick='alert(\"hi\")>George</b> tag, or the <a href='monkey.html'>I have a pet monkey.</a> tag.
textcomment == "Hello, my name is George and I live in the jungle. They call me George of the Jungle."