Skip to content

Instantly share code, notes, and snippets.

@tan-yuki
Created December 23, 2012 05:53
Show Gist options
  • Save tan-yuki/4362208 to your computer and use it in GitHub Desktop.
Save tan-yuki/4362208 to your computer and use it in GitHub Desktop.
/**
* Sanitizing string value.
*
* @require jQuery
*/
(function($) {
var $div = $('<div/>');
String.prototype.sanitize = function() {
return $div.text(this)[0].innerHTML;
};
}) (jQuery);
/*
* var html = '<script>alert("hello");</script>';
* html.sanitize(); // === "&lt;script&gt;alert("hello");&lt;/script&gt;"
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment