I'm working on a project, and I didn't understand why a call to external script doesn't work.
Then I just did a extremely simple page html which includes a script alert, as you can see below... Can you tell me what's the problem ? I believe the problem is not the code, but what else can it be?
My browser is a recent Chrome, and my OS is Ubuntu. My HTML file is index.html :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>MyPage</title>
</head>
<body>
<p>Blablabla</p>
<script type="text/javascript" src="/script.js"></script>
</body>
</html>
The Javascript file is script.js in the same folder:
<script type="text/javascript">
alert('Hey');
</script>
I'm working on a project, and I didn't understand why a call to external script doesn't work.
Then I just did a extremely simple page html which includes a script alert, as you can see below... Can you tell me what's the problem ? I believe the problem is not the code, but what else can it be?
My browser is a recent Chrome, and my OS is Ubuntu. My HTML file is index.html :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>MyPage</title>
</head>
<body>
<p>Blablabla</p>
<script type="text/javascript" src="/script.js"></script>
</body>
</html>
The Javascript file is script.js in the same folder:
<script type="text/javascript">
alert('Hey');
</script>
Share
Improve this question
edited Nov 21, 2017 at 22:09
Mureinik
312k54 gold badges353 silver badges388 bronze badges
asked Nov 21, 2017 at 22:04
florianflorian
3872 gold badges4 silver badges15 bronze badges
5
-
2
can you try
src="./script.js"
? – Alberto Rivera Commented Nov 21, 2017 at 22:05 -
5
if that second piece of code is your script file, remove the HTML from javascript files - i.e. you don't need
<script ....>
</script>
inside a .js file – Jaromanda X Commented Nov 21, 2017 at 22:06 -
Did you try debugging? If there is a JS error it will show in the
console
section. – IE5Master Commented Nov 21, 2017 at 22:06 -
1
Your
script.js
file contains HTML, not JavaScript. This would result in a syntax error. Check your browser's debugging console. To correct it, remove the HTML tags from the JavaScript file. While you're in the browser's debugging tools, take a look at the network tab and see if the request for the.js
file was successful. Take a look at the script tab and see if it's available. – David Commented Nov 21, 2017 at 22:08 - Generally it's good practice to put script tags in the header. You can read more here: stackoverflow./questions/436411/… – Joseph Cho Commented Nov 21, 2017 at 22:12
5 Answers
Reset to default 2Paths starting with /
are absolute paths. If the script and the HTML page are in the same directory, the script's path is simply "script.js":
<script type="text/javascript" src="script.js"></script>
<!-- Here --------------------------^ -->
If the file is in the same folder remove the "/" from script.js
<script type="text/javascript" src="script.js"></script>
Also if the js file has the script tags remove them.
If you want the alert when the doc is ready, consider doing something like:
document.addEventListener("DOMContentLoaded", function(event) {
alert('Hey')
});
I think that the script in the file dosen't need this script tag
<script type="text/javascript">
alert('Hey');
</script>
you can make like this
alert('hey');
just that try out and check if the file path in html to the js file is the right one.
Hi you don't need the script tags in the file:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>MyPage</title>
</head>
<body>
<p>Blablabla</p>
<script type="text/javascript" src="/script.js"></script>
</body>
</html>
The Javascript file is script.js in the same folder:
alert('Hey');
I have solve this problem by using Visual Studio. Just open the html file in VS and then run this file from here. It will connect your js file to html.