简体   繁体   English

在 JavaScript 文件中放置字符集不起作用

[英]Placing charset in the JavaScript file doesn't work

I have such a problem.我有这样的问题。 My script doesn't load external file properly.我的脚本没有正确加载外部文件。 An error is: func() is not defined.错误是:func() 未定义。 And the code:和代码:

external.js:外部.js:

@charset "UTF-8"; 
function func(){
alert("0ddd");
}

and the script:和脚本:

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="external.js"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="center">
<h1 align="center">My site</h2>
<script>func()</script>
</div>
</body>
</html>

My files are placed in the same folder.我的文件放在同一个文件夹中。 Regards问候

JavaScript file shouldn't contain such character. JavaScript 文件不应包含此类字符。 Remove it and will work fine:删除它,将正常工作:

@charset "UTF-8"; // remove it

Also, you may place the script inside the body rather:此外,您可以将脚本放在正文中:

<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="center">
<h1 align="center">My site</h2>
</div>
<script type="text/javascript" src="external.js"></script>
<script>func()</script>
</body>
</html>

If you want to explicitly set the charset , then add it in the script tag itself:如果要显式设置charset ,请将其添加到脚本标记本身中:

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

But it is better to provide charset in the meta tag: (Place the following inside the <head>但是最好在元标记中提供charset :(将以下内容放在<head>

<meta charset="UTF-8">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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