How to transform accented characters in html using Javascript?
I'm using a开发者_如何学运维 form to record data in my database using AJAX. So, how this works? I get the form data via Javascript, request a URL which is written in PHP then insert to the database.
The problem is that I'm using Latin 1 characters so the form is expecting also accented characters like ó. I need to convert this ó to html (ó
) in javascript, so that I can pass it to PHP.
How doI do that?
I think the best way to do it is to make an AJAX request to a PHP page outputting the result of htmlentities()
.
This should do it for you:
function escapeHTMLEncode(str) {
var div = document.createElement(‘div’);
var text = document.createTextNode(str);
div.appendChild(text);
return div.innerHTML;
}
http://sanzon.wordpress.com/2008/05/01/neat-little-html-encoding-trick-in-javascript/
精彩评论