Encoding of included JSON files
Lately I had a problem with a page which was including two JSON scripts, one encoded in UTF-8 and the other encoded in ISO-8859-1.
In some circumstances (and in some browsers) the strings where not showed correctly.
Hunting down the problem required a good dose of my favourite tool wget -S where the -S option causes the http response headers to be printed on stderr.
My conclusions are that:
In other words, to make a script inclusion cross-browser and independent of the encoding of the page you are including the script, always use the following if your page is using iso-8859-1
[source:xml]
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<script src="latin1_strings.js" type="text/javascript" charset="iso-8859-1"></script>
<script src="utf-8_strings.js" type="text/javascript" charset="utf-8"></script>
[/source]
or
[source:html]
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="latin1_strings.js" type="text/javascript" charset="iso-8859-1"></script>
<script src="utf-8_strings.js" type="text/javascript" charset="utf-8"></script>
[/source]
if your page is utf-8.
And of corse, make sure your http headers match the content!
In some circumstances (and in some browsers) the strings where not showed correctly.
Hunting down the problem required a good dose of my favourite tool wget -S where the -S option causes the http response headers to be printed on stderr.
My conclusions are that:
- if a page uses a certain encoding (as specified by the charset on the page and/or the Content-Type http header) the included javascript will be handled with the same type of encoding if nothing else is specified
- In Firefox, if the http response of the javascript contains the "Content-Type" header with a charset value, the file will be treated as using this encoding
- In Internet Explorer 6 (and 7?) the http headers are ignored and you must explicitly use the charsets attribute of the script tag to force the correct encoding
In other words, to make a script inclusion cross-browser and independent of the encoding of the page you are including the script, always use the following if your page is using iso-8859-1
[source:xml]
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<script src="latin1_strings.js" type="text/javascript" charset="iso-8859-1"></script>
<script src="utf-8_strings.js" type="text/javascript" charset="utf-8"></script>
[/source]
or
[source:html]
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="latin1_strings.js" type="text/javascript" charset="iso-8859-1"></script>
<script src="utf-8_strings.js" type="text/javascript" charset="utf-8"></script>
[/source]
if your page is utf-8.
And of corse, make sure your http headers match the content!
Comments
Post a Comment