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
2 Answers
Reset to default 4You 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>