I have unescaped data from users.
So is it safe to use like this:
var data = '<test>a&f"#</test>'; // example data from ajax response
if (typeof(data) === 'string')
$('body').text(data);
Can I use like this or there is some problems like encoding or some specific symbols that I should be careful and add more strict validation?
When you set the text of an element using the
textmethod, jQuery usescreateTextNodeinternally, which escapes all special characters.From the jQuery docs:
So yes, it should be safe. Here’s your example in jsfiddle. Notice how the tags appear as literal text.