5

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>
Mureinik
  • 297,002
  • 52
  • 306
  • 350
florian
  • 367
  • 2
  • 4
  • 14
  • 2
    can you try `src="./script.js"` ? – Alberto Rivera Nov 21 '17 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 `` inside a .js file – Jaromanda X Nov 21 '17 at 22:06
  • Did you try debugging? If there is a JS error it will show in the `console` section. – IE5Master Nov 21 '17 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 Nov 21 '17 at 22:08
  • Generally it's good practice to put script tags in the header. You can read more here: https://stackoverflow.com/questions/436411/where-should-i-put-script-tags-in-html-markup – Joseph Cho Nov 21 '17 at 22:12

5 Answers5

2

Paths 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 --------------------------^ -->
Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

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')
});
FutoRicky
  • 903
  • 2
  • 9
  • 22
  • I have changed what you say, but I still have the same problem – florian Nov 21 '17 at 22:11
  • 2
    @florian did you remove the script tag from the javascript file? – FutoRicky Nov 21 '17 at 22:14
  • 1
    Oh damned ! I only removed type="text/javascript" Now, it works without – florian Nov 21 '17 at 22:18
1

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.

0

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');
DCR
  • 14,737
  • 12
  • 52
  • 115
0

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.

Gaur 97
  • 73
  • 5
  • This doesn't address their actual code, though. There should be ways to debug the code without using a specific text editor – camille Apr 14 '20 at 19:09
  • Check this post might help if its angular application https://stackoverflow.com/questions/56585794/why-angular-is-not-able-to-find-app-js-file-and-throwing-up-404-error – Mahesh Revaskar Dec 15 '22 at 06:10