Reputation: 209
I have a chat application wirtten in javascript, when a user enters an html code like
<button>click</button>
a button appears on chat. I use this code
if(message.indexOf('<button') != -1)
{
message = message.replace(message, '-');
}
but this just replaces the < button
with a blank space, I want it to be displayed as text and not an actual button.
Thanks
Upvotes: 2
Views: 2409
Reputation: 34895
Your general encode procedure here would be:
value = value.toString().replace(/</g, "<").replace(/>/g, ">").replace(/'/g, "'").replace(/"/g, """);
Upvotes: 1
Reputation: 148
what html element did you use for print the chat? try use textarea or pre tag.
try to control those errors in the back end because everybody can deactivate javascript.
Upvotes: 0
Reputation: 324610
I just use this:
message = message.replace(/</g,"<");
That's all that's needed to prevent HTML from being inserted.
Upvotes: 7