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.
Since you're using jQuery, you can just set the element's
|
|||||||||||||||||||||
|
Source: http://debuggable.com/posts/encode-html-entities-with-jquery:480f4dd6-13cc-4ce9-8071-4710cbdd56cb |
|||||||||||||
|
There is also the solution from mustache.js https://github.com/janl/mustache.js/blob/master/mustache.js#L82
|
|||||||
|
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 " to
|
|||||||||||||||||||||
|
I wrote a tiny little function which does this. It only escapes
And the function uses a closure to keep 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 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 un*escaping. 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 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 C Akfohg Apr 20 at 1:29 |
|
Re ' : correct: safe use only in XHTML; straight from the crowd-source's mouth - emphasis mine: "(...) read by a conforming HTML processor, (...) use of ' or custom entity references may not be supported (...)" - in practice: modern browsers support it even in HTML.
Re case in hex nums. (same source; emphasis mine):"The x must be lowercase in XML documents. […] The hhhh may mix uppercase and lowercase, though uppercase is the usual style."
Leaves us to wonder who decided to encode slashes; perhaps really just a confusion between URI and HTML encoding? – mklement0 Apr 20 at 2:34 |
Try Underscore.string lib, it works with jQuery.
output:
|
|||||
|
Here is a clean clear javascript function.
|
|||||||
|
If your're going the regex route, there's an error in tghw's example above.
|
|||||
|
I've enhanced the mustache.js example adding the
That way it is quite easy to use |
|||
|
This is a nice safe example...
|
||||
|
escape() and unescape() are intended to encode/decode strings for URLs, not HTML. Actually, I use the following snippet to do the trick that doesn't require any framework:
|
||||
|
Escaped string: "It%27s%20%3E%2020%25%20less%20complicated%20this%20way." If the escaped spaces bother you, try:
Escaped string: "It%27s %3E 20%25 less complicated this way." Note: This is only for easy embedding and doesn't break embedded HTML and scripts, for which this answer provides the jQuery and normal JS methods. |
||||
|
|
||||
|
No global variables, some memory optimization. Usage:
result is:
|
|||
|
works like a charm |
|||
|
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). |
||||
|