简体   繁体   中英

How to set charset=“utf-8” in the javascript file itself

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

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM