简体   繁体   English

如何正确将javascript文件包含到html中

[英]How to correctly include a javascript file into html

I am trying to run a script through HTML but I am having some problems. 我正在尝试通过HTML运行脚本,但是遇到了一些问题。 I searched online and found how to do so, however the issue is that even if I correctly type the path of the .js file, it seems to add some strange characters before it. 我在网上搜索并找到了解决方法,但是问题是,即使我正确输入了.js文件的路径,似乎也要在它之前添加一些奇怪的字符。

This is in index.html 这是在index.html中

<script type="text/javascript" src="fractalTest/fractalTest.js"></script>

I expected this to work but when I open index.html in google chrome and inspect then look under the elements tab, this "â©fractalTest/fractalTest.js" is replacing "fractalTest/fractalTest.js" in the path of the file. 我希望它能起作用,但是当我在Google chrome中打开index.html并检查然后在elements选项卡下查看时,此“âfractalTest/ fractalTest.js”将替换文件路径中的“ fractalTest / fractalTest.js”。 I believe this is whats causing the error but I do not know how to fix it! 我相信这是导致错误的原因,但我不知道如何解决!

...it seems to add some strange characters before it. ...似乎在它之前添加了一些奇怪的字符。

That usually means that the file is saved with a byte-order mark (BOM) of some kind, but isn't being sent with the correct charset for that byte-order mark. 这通常意味着该文件以某种字节顺序标记 (BOM)保存,但没有以该字节顺序标记的正确charset发送。

Be sure that the server is configured to serve the files with a specific encoding (UTF-8 is a good choice), and that you save the files using that encoding (in your text editor, etc.). 确保将服务器配置为使用特定的编码来提供文件(UTF-8是一个不错的选择),并确保使用该编码保存文件(在文本编辑器等中)。 It's also usually best not to include a BOM on UTF-8 files (although it's valid, some tools don't handle it well). 通常也最好不要在UTF-8文件中包含BOM(尽管它是有效的,但是某些工具不能很好地处理它)。


Side note: No need for the type attribute. 旁注:不需要type属性。 The default is JavaScript. 默认值为JavaScript。

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

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