views:

126

answers:

1

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 dinamically include Tst.js the characters go bannanas...

And unfortunately I can't port all my code to UTF-8 :(

Please help, I have cookies :)

+2  A: 

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>
Pekka
Yes, both files are in windows-1250. That's why it is so strange.
Stazh
Now I've changed the order of charset and src attributes so the charset is before src but the problem persists.
Stazh
@Stazh Really strange. What happens if you add `charset="windows-1250"` to example 2 (where you include the script using a script tag)?
Pekka
Nothing :) It works fine :) And when I set charset to something different it also works and renders characters fine. It's like the charset attr is ignored.And when I load the script dynamically it's like it is beeing loaded to some separate page with unset or wrongly set charset. But strange enough, the functions from Tst.js if I add them work just fine, with no problems, only with Croatian characters.
Stazh
@Stazh You mention Webkit so I'm not 100% sure it's relevant but still: "Currently Netscape/Mozilla does not fully support the W3C HTML standard charset attribute in resource links; e.g.: Anchor, LINK, SCRIPT." http://www-archive.mozilla.org/quality/intl/browser/charsethandling/testcase-CharsetHandlingII.html Ideas to try: 1. encoding the script file as UTF-8 and setting the `charset` property accordingly (to try out whether it's limited to UTF-8 and ISO) 2. sending a `content-type` header in the js embed file, using web server settings or a scripting language like PHP.
Pekka
@Stazh and by the way, there is no AJAX involved here is it? Because Ajax requests go through as utf-8 by default.
Pekka
@Pekka: Yes, when I set the header explicitly to charset=windows-1250 it works just fine!Thank you Pekka very much :)( I'm new here to stackoverflow.com so is there any system of reputation here so i can give you rep+ or something? )
Stazh
@Stazh cheers, glad it works and this is very good to know! Upvoting and accepting answers is the way of giving reputation around here, so everything's fine.
Pekka
Oh and @Stazh make sure you check whether setting the header works for IE 6 / 7 / 8 to avoid surprises.
Pekka
I'm creating web application not web site so I have the privilege to mandate what kind of browser must be used with the web app and decided not to support IE 6 and 7 :)Thanks a lot again :)
Stazh