How to set charset="utf-8" in the javascript file itself
Asked Answered
V

4

27

I am trying to set charset="utf-8" inside the javascript file itself, not in the script tag, I know that I can do this:

<script type="text/javascript" charset="UTF-8" src="xyz.js"></script>

But unfortunately, this solution needs me to do the same step with hundreds of websites which are using the same script. so I am trying to set the charset in the javascript file itself. Is this possible?

thanks

Vida answered 17/6, 2017 at 15:13 Comment(5)
Just use utf-8 encoding when you create the file. Really not clear what higher level problem you are trying to solveJehius
@Jehius I'm creating the file using fwrite() in php, I tried to use UTF-8 with fwrite() but documentations on the subject not too much. Anyway, when I save js file to Amazon WS, I can see the file support Arabic very well but when websites(which using that js file) loading that js file, I can see in the chrome inspector/ resources section that the js file does not support UTF-8/arabic.Vida
@Jehius I downloaded the file from AWS then re-save it as UTF-8, and re-upload it and still the same.Vida
If you save the JS file with UTF-8, then you must declare charset="UTF-8" in the <script> tag of the HTML file. Otherwise, most browsers will decode the JS file as ISO 8859-1, which does not include Arabic characters. And you have to do this in every HTML file that references this JS file, there's no other mechanism to let the browser know the encoding of the JS file, as far as I know.Duckweed
@Duckweed thank you man, yeah it is as you said, I found work around that can solve it for now.Vida
V
32

I found another way, so instead of declaring charset="UTF-8" for the script tag like this:

<script type="text/javascript" charset="UTF-8" src="xyz.js"></script>

I can declare the charset for the web page itself using meta tag, so I can append <meta charset="UTF-8"> to the DOM dynamically, and end up with something like:

<head>
...
  <meta charset="UTF-8">
...
</head>
Vida answered 4/7, 2017 at 13:43 Comment(0)
B
3

I think you can't set this in the Javascript file itself. The browser need the charset to read the file. So without the charset the browser is not able to understand your file and therefore would not be able to read the charset definition

Brawny answered 17/6, 2017 at 15:19 Comment(1)
It doesn't stop HTML from declaring the encoding inside the file itselfDiapason
S
0

Set nginx.conf like this:

http {
    charset utf-8;

    types {
        text/javascript utf-8 js;
    }
    
    # Other configuration settings...
}

Emojis in .js files now get displayed properly. (In source view, they worked just fine before)

Stilton answered 20/8, 2023 at 6:44 Comment(0)
J
0

FYI: It is rarely used nowadays because UTF-8 is the default encoding for most web documents.

Jody answered 27/3, 2024 at 1:52 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewPunchball

© 2022 - 2025 — McMap. All rights reserved.