print html tags in javascript -


thanks reading!

var data = "<html><head><title>hello</title></head><body>hello body</body></html>"; 

i want print data including html tags without having browser rendering html tags , displaying "hello body".

i tried:

str = str.replace("<", "");

but in vain.

 data = data.replace(/</g, "&lt;").replace(/>/g, "&gt;"); 

when browser encounters &lt; (which known character entity), replace literal '<', enabling display html tags on page without them getting rendered.

/</g regular expression says "match '<' in string", , g means globally. without g replace first '<' encounters.

and 1 final note, it's better use library, such jquery, this. kind of stuff easy wrong , miss edge cases on. let hardened, tested , secure library function you.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -