When I copy and paste some code snippet on this blog, I have to escape some special characters for example "<", ">", etc.
I googled how to escape html special characters by javascript and found escaping-html-strings-with-jquery.
Based on the answer, I created simple html escape tool below.
I googled how to escape html special characters by javascript and found escaping-html-strings-with-jquery.
Based on the answer, I created simple html escape tool below.
The whole javascript code is below!
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(function(){
$("#src_html").keyup(function(){
$("#dest_html").val(escapeHtml($("#src_html").val()));
});
var entityMap = {
"&": "&",
"<": "<",
">": ">",
'"': '"',
"'": ''',
"/": '/'
};
function escapeHtml(string) {
return String(string).replace(/[&<>"'\/]/g, function (s) {
return entityMap[s];
});
}
});
</script>
コメント