Dynamically loaded JavaScript file and non-ASCII characters
I have the following problem:
<script type="text/javascript">
alert("1. ČĆŽŠĐčćžšđ");
</script>
<script type="text/javascript" src="Tst.js"></script>
<script type="text/javascript">
var pScript = document.createElement("script"开发者_运维知识库);
pScript.type = "text/javascript";
pScript.src = "Tst.js";
pScript.charset = "windows-1250";
$("body").append(pScript);
</script>
(These are Croatian characters.)
Contents of Tst.js is:
alert("2. ČĆŽŠĐčćžšđ");
Output of this script in FireFox (and Safari, so I've concluded that this is not the problem with the browser, but my code):
1. ČĆŽŠĐčćžšđ
2. ČĆŽŠĐčćžšđ
2. �Ǝ���枚�
Charset on the main page that is calling this code is Windows-1250.
I don't understand why when I call Tst.js statically (by <script src="Tst.js" type="text/javascript"></scipt>
) the characters are shown normal, but when I dynamically include Tst.js the characters go bannanas...
And unfortunately I can't port all my code to UTF-8.
Any advice?
2nd update: Specifying the encoding in the content-type header of the JavaScript file did the trick - for whatever reason!
Update: You are setting the character set after loading the script. Try
<script type="text/javascript">
var pScript = document.createElement("script");
pScript.type = "text/javascript";
pScript.charset = "windows-1250";
pScript.src = "Tst.js";
$("body").append(pScript);
</script>
精彩评论