最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you import a local js file into a basic html file? - Stack Overflow

programmeradmin2浏览0评论

I have a very basic html file that uses a script tag to import a javascript file, but the javascript file is not run when I load the html locally in a browser. How am I formatting the script tag incorrectly?

Folder Structure

-folder
  --test.html
  --script.js

test.html

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>TEST</title>
</head>

<body>
  <p>TEST</p>
  <script type='text/javascript' src="script.js" />
</body>
</html>

script.js

console.log('HELLO WORLD')

(function () {
  setTimeout(() => alert('HELLO WORLD'), 3000)
})();

I have a very basic html file that uses a script tag to import a javascript file, but the javascript file is not run when I load the html locally in a browser. How am I formatting the script tag incorrectly?

Folder Structure

-folder
  --test.html
  --script.js

test.html

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>TEST</title>
</head>

<body>
  <p>TEST</p>
  <script type='text/javascript' src="script.js" />
</body>
</html>

script.js

console.log('HELLO WORLD')

(function () {
  setTimeout(() => alert('HELLO WORLD'), 3000)
})();
Share Improve this question asked Jan 27, 2021 at 22:06 Jon_BJon_B 1,0796 gold badges19 silver badges27 bronze badges 2
  • Does this answer your question? How to run a function when the page is loaded? – R Greenstreet Commented Jan 27, 2021 at 22:10
  • You can't use self-closing tags for <script>. See stackoverflow./questions/69913/… – Daniel Beck Commented Jan 27, 2021 at 22:33
Add a ment  | 

2 Answers 2

Reset to default 4

You should close your script tag in a separate tag :

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

Your script tag is incorrect it should be : <script type='text/javascript' src="script.js"></script>

发布评论

评论列表(0)

  1. 暂无评论