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, "<").replace(/>/g, ">"); when browser encounters < (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
Post a Comment