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:
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.phpUse 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?