javascripthtmlurldecode

to decode accented letters in the url with javascript?


Hello guys i have this form:

<form action='file:///C:/Users/amministratore/Desktop/google_cse/ricerca.html' method="get" target='_blank' id='cse-search-box'>
    <input type='text' name='q' size='35' />            
    <input type="submit" name="sa" value="Search" />
</form>

e this code js:

var linkcodificato= self.location.href;
var link= decodeURIComponent(linkcodificato);

I can decode many characters (? |! And many others) but not the accented letters ... why?

à = %E0

EDIT

In my search bar i search:

"hello à"

the url that i have is this:

?q=Hello+%E0

Solution

  • The problem is that %E0 represents the á character in ANSI (Windows-1252) encoding, while it is invalid in utf-8.

    You can avoid this behaviour by making sure that:

    With this set-up you'll find that the á character just stays as-is in the url. However, the location.href will still be encoded, but if you did the above, the encoding will now have %C3%A0 instead of the offending %E0, and that will nicely convert to à when you use decodeURIComponent.

    Whenever you make changes, make sure to refresh the original page, and to resubmit the form.

    Here is a suggested start of your html code:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
    </head>
    

    With the above you ensure that your document runs in utf-8 mode.