Charset使用javascript小部件编码到php

I'm creating a Javascript widget which is a button that redirects a query string to a php page in a popup. The page that will integrate the widget sends information to the Javascript widget first using this method:

<script src="http://api.mydomain.com/widget/widget.js?api_key=123&firstname=Veronica&lastname=Gällman" type="text/javascript"></script>

The widget reads the strings submitted and then creates the popup/iframe and redirects to http://api.mydomain.com/widget/index.php?firstname=Veronica&lastname=Gällman

The problem occurs when the page that included the JS widget does NOT have charset=utf-8. The special characters will become question marks, or boxes.

My programmer wants me to do do it like this:

<script type="text/javascript">
    var search = 'api_key='+encodeURIComponent('1234')+
        '&firstname='+encodeURIComponent('Veronica')+
        '&lastname='+encodeURIComponent('Gällman')+
    document.write( search );
</script>

But I find this approach problematic for the person who wants to use the widget, and not elegant at all.

So finally, my question: Is there a better way to solve this?

The script should support pages which have charset=utf-8 and charset=iso-8859-1 on them.

My suggestions to him were these:

  1. Force integrator to send &charset=iso-8859-1 in the query string, if it is not utf-8, and then convert the ISO to UTF-8 in index.php

  2. Use a library to detect the character encoding. If it is detected as ISO, convert it into UTF-8 in index.php

Do any of these sound reasonable? Is number 2 possible? Or is there a better solution to this?

Am I forced to tell my widget users to urlencode on their page?

You can detect encoding using mb_detect_encoding function and convert it with mb_convert_encoding or iconv.

By the way, can't you make a world a better place by forcing people to use UTF-8? :)

As Donatas already mentioned, it's better to force people -especially on the web!- to use UTF8 for quite everything.

But in case you need to convert a UTF8 string to ISO-8859-1, try http://phpjs.org/functions/utf8_decode ...

PS: Remember, that not all UTF8 characters can be represented in ISO-8859-1 notation!