简体   繁体   English

使用 .js 文件导入 html<script>

[英]Importing .js file into html using <script>

I've looked at different posts on StackOverflow but I can't seem to solve this problem despite following the suggestions.我在 StackOverflow 上查看了不同的帖子,但尽管遵循了建议,但我似乎无法解决这个问题。 Any help would be appreciated.任何帮助,将不胜感激。

I have the following HTML code in the file home.html in my frontend folder:我的前端文件夹中的文件 home.html 中有以下 HTML 代码:

`<!DOCTYPE html>
  <html lang="en">
  <head>
  <title> Home Page </title>
   <meta charset="utf-8">
   <script src ="src/home.js"></script>
  </head> 
  </html>`

And I have a file home.js in the folder src that I'm trying to import.我在文件夹 src 中有一个文件 home.js 我试图导入。 However, when I then run localhost I get the error:但是,当我然后运行 ​​localhost 我收到错误:

The resource from “ http://localhost:3000/src/home.js ” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff)由于 MIME 类型(“text/html”)不匹配,来自“ http://localhost:3000/src/home.js ”的资源被阻止(X-Content-Type-Options:nosniff)

I've tried adding我试过添加

app.use("/src", express.static(path.join(__dirname, "../src"))); app.use("/frontend", express.static(path.join(__dirname, "../frontend")));

to my server.js file but it doesn't help.到我的 server.js 文件,但它没有帮助。

Thanks in advance!提前致谢!

添加“类型”属性,如下所示。

<script type="text/javascript" src="path-to-javascript-file.js"></script>

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

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