drkbpwk609294 2012-08-03 19:29
浏览 26

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?

  • 写回答

2条回答 默认 最新

  • drbii0359 2012-08-03 20:07
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等