Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch utf parameter from url?

I got from server words which contains utf characters like Ž,ć and so on.... I put in url parameters and my url looks like ?id=229&name=%8eena%20mini%3f

I am fetching parameters from url with js function

function getURLParameter(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

and it parses first id parameter but on second (when I have utf before encoded in url) it breaks.

var id = getURLParameter('id');//works
var id = getURLParameter('name');//breaks when have utf

How to fetch that parameter when it has utf from url ? (On page I have <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">)

like image 675
Damir Avatar asked Nov 29 '25 21:11

Damir


1 Answers

Whatever is encoding Žena to %8eena is not using UTF-8 encoding, it's using Windows-1252 enoding, since Ž is represented as 8e in that character set.

In UTF-8, Ž is represented as c5 bd, so you should expect your URL to contain %c5%bd if the form had the correct encoding.

Looks like you need <meta charset="utf-8"> on the previous page, or at the very least the attribute accept-charset="utf-8" on the form that you submit.

See also: Is there any benefit to adding accept-charset="UTF-8" to HTML forms, if the page is already in UTF-8?

like image 79
cmbuckley Avatar answered Dec 02 '25 12:12

cmbuckley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!