dtlygweb2017 2012-12-13 14:23
浏览 75

HTTP与HTML字符集影响JS包含

I'm changing a website to use the UTF-8 character set from iso-8859-1.

When i change this:

<meta http-equiv='content-type' content='text/html; charset=iso-8859-1' />

to

<meta http-equiv='content-type' content='text/html; charset=utf-8' />

It all appears to work fine and posting forms sends the data in UTF-8. Perfect.

But when i set this

<?php header('Content-Type: text/html; charset=utf-8'); ?>

The javascript includes break which i wasn't expecting. Obviously there is some 3rd party code in their that uses characters in iso-8859-1 encoding that doesn't change to utf-8 correctly. So fixing the javascript code itself is easy enough.

HTTP Header Set to UTF-8 + HTML Content Type Set to UTF-8

  • Breaks javascript includes
  • Affects the encoding of posting form data

As expected, it hints that everything is using UTF-8, however, it breaks the javascript include... It also only breaks the js include on FireFox. I tested this configuration on IE, Chrome and Opera and it works fine. For this reason i'm starting to think maybe FireFox is at fault here and this is a bug in the browser (though a simple fix for FireFox is to add chartset='iso-8859-1' to the script tag).

HTTP Header set to iso-8859-1 + HTML Content Type Set to UTF-8

  • Does not affect javascript includes
  • But does affect the encoding of posting form data

I was always under the impression that the HTTP header SHOULD overwrite any inconsistant character sets defined in the HTML header. However, it appears JS includes listen to the HTTP character set... while posting data ignores the HTTP header and listens to the HTML character set instead!?

Can anybody explain why and is it supposed to happen this way or is it possibly a bug in FireFox 17.0.1?

  • 写回答

1条回答 默认 最新

  • duanqin9631 2012-12-13 14:34
    关注

    If you can control the buffering, use http://php.net/manual/en/function.utf8-encode.php else, just alter the Javascript and make it UFT-8.

    About the question... the php header controls the output sent by the server so, the PHP will alter the content. That META tag is just an info for the browser "be careful, this is UTF-8".

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line