Encoding and JS – how to remove accents with javascript from a UTF-8 text

I have this option on a webpage, that double clicking on a word, it opens a new window and performs a search. I used a short javascript function (like RemoveAccents() or accentsTidy()) to remove accents from the word, and pass the word as a string in the new window’s url. Now I changed the encoding of the site and use UTF-8 encoding in the declaration, and encode the text with UTF-8, as it’s mostly in French, using the PHP function iconv(“ISO-8859-1”, “UTF-8”, $string) (iconv(“Windows-1252″…) doesn’t solve the problem).

But with UTF-8 encoding, the javascript function does not remove the accents, and the new window is blank when the url has accents in it.

It works fine when I switch back to ISO-8859-1 encoding for the page.

I tested the functions with alert(string) and they work fine except for the accents which are not replaced with their equivalent letter, the only error message on the JS log of the new window is about the encoding not being declared, which is obvious as the page is blank when there is an accent in the url…

I also tested with accents in the url on other pages and I don’t see any problem, if not for the needed decoding/encoding that follows.

Source: javascript

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.