2

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?

2 Answers 2

1

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? :)

Sign up to request clarification or add additional context in comments.

2 Comments

The question remains. What does the integrating website need to do, and is there a better solution?
Sorry I can't offer a solution. It's already 8 or 9 years since I started using UTF-8 only and nobody else in my country and in neighbour countries use other encodings (well, except ASCII, but that's not the case).
1

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!

2 Comments

The problem is not converting strings.
Okay, I'm sorry for that. You might want to use the character set specified for the used website. It's possible through document.characterSet. Please read developer.mozilla.org/en-US/docs/DOM/document.characterSet for further information. Use document.charset for IE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.