Does anyone know of an easy way to escape HTML from strings in jQuery? I need to be able to pass an arbitrary string and have it properly escaped for display in an HTML page (preventing JavaScript/HTML injection attacks). I'm sure it's possible to extend jQuery to do this, but I don't know enough about the framework at the moment to accomplish this.
Join them; it only takes a minute:
|
Since you're using jQuery, you can just set the element's
|
|||||||||||||||||||||
|
There is also the solution from mustache.js
|
|||||||||||||||||||||
|
Source: http://debuggable.com/posts/encode-html-entities-with-jquery:480f4dd6-13cc-4ce9-8071-4710cbdd56cb |
|||||||||||||||||||||
|
If you're escaping for HTML, there are only three that I can think of that would be really necessary:
Depending on your use case, you might also need to do things like
|
|||||||||||||||||||||
|
I wrote a tiny little function which does this. It only escapes
This is plain Javascript, no jQuery used. Escaping
|
|
Nicely done; would you mind amending it to also escape
' and / ? that would make it a more concise, self-contained alternative to @Tom Gruner's mustache source code-based solution.
– mklement0
Apr 12 '13 at 14:15
|
||
|
|||
|
Thank you for taking the time, @Zrajm. Good point about not needing escaping; any idea why both
mustache.js and underscore.js do it? Speaking of the latter: it only recognizes the numerical entities (representing ' and / '), in the uppercase hex form when unescaping. Thus, text escaped in mustache.js - which curiously uses a mix of hex. and decimal formats - would not be correctly unescaped in underscore.js . I wonder how other popular libraries deal with that.
– mklement0
Apr 18 '13 at 13:22
|
||
|
The lower case hex form is the most supported form, so that is (probably) the form that the libraries should convert to. (Of course both forms should work when converting from.) – Apostrophes
' have some sort of reserved function in XML (and thus XHTML, I imagine?), which is why XML (but not HTML) have the named entity ' . Exactly in why or in what way it is “reserved” I do not know. – Slashes are special in URLs, but that does not actually warrant them for inclusion in escaping HTML (as URL encoding is something completely different).
– zrajm
Apr 20 '13 at 1:29
|
||
|
Final thoughts: seems like encoding
/ is not needed, but encoding ' still seems useful to safely handle the case case where an encoded string is used as an attribute value enclosed in single quotes.
– mklement0
Apr 20 '13 at 3:05
|
Easy enough to use underscore:
Underscore is a utility library that provides a lot of features that native js doesn't provide. There's also lodash which is the same API as underscore but was rewritten to be more performant. |
||||
|
Try Underscore.string lib, it works with jQuery.
output:
|
|||||
|
Here is a clean, clear JavaScript function. It will escape text such as "a few < many" into "a few < many".
|
|||||||||
|
After last tests I can recommend fastest and completely cross browser compatible native java script (DOM) solution:
If you repeat it many times you can do it with once prepared variables:
Look at my final performance comparison (stack question). |
|||||||||||||
|
I've enhanced the mustache.js example adding the
That way it is quite easy to use |
|||
|
Actually, I use the following snippet to do the trick that doesn't require any framework:
|
|||||
|
I realize how late I am to this party, but I have a very easy solution that does not require jQuery.
Edit: This does not escape quotes. The only case where quotes would need to be escaped is if the content is going to be pasted inline to an attribute within an HTML string. It is hard for me to imagine a case where doing this would be good design. |
|||||||||||||
|
If you have underscore.js, use
|
||||
|
If your're going the regex route, there's an error in tghw's example above.
|
|||||
|
This is a nice safe example...
|
|||||
|
No global variables, some memory optimization. Usage:
result is:
|
|||
|
|
|||||
|
You can easily do it with vanilla js. Simply add a text node the document. It will be escaped by the browser.
|
|||
|
2 simple methods that require NO JQUERY... You can encode all characters in your string like this:
Or just target the main characters to worry about
|
||||
|
works like a charm |
|||||
|
This answer provides the jQuery and normal JS methods, but this is shortest without using the DOM:
Escaped string: If the escaped spaces bother you, try:
Escaped string: Unfortunately, the
All major browsers still support the short code, and given the number of old websites, i doubt that will change soon. |
|||||||||
|
If you are saving this information in a database, its wrong to escape HTML using a client-side script, this should be done in the server. Otherwise its easy to bypass your XSS protection. To make my point clear, here is a exemple using one of the answers: Lets say you are using the function escapeHtml to escape the Html from a comment in your blog and then posting it to your server.
The user could:
If the user paste this snippet in the console it would bypass the XSS validation:
|
|||||||||||||||||||||
|
All solutions are useless if you dont prevent re-escape, e.g. most solutions would keep escaping
|
|||||
|
protected by Michael Berkowski May 26 '14 at 19:27
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?