8

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>
5
  • 2
    can you try src="./script.js" ? 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 Commented Nov 21, 2017 at 22:06
  • Did you try debugging? If there is a JS error it will show in the console section. 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. 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.com/questions/436411/… Commented Nov 21, 2017 at 22:12

5 Answers 5

3

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 --------------------------^ -->
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I change the line with <script type="text/javascript" src="script.js"></script> still the same problem
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')
});

3 Comments

I have changed what you say, but I still have the same problem
@florian did you remove the script tag from the javascript file?
Oh damned ! I only removed type="text/javascript" Now, it works without <script> tag :') Thank you so much, I know it was a stupid question, but I'm beginning Javascript at school ahah !
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.

Comments

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');

Comments

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.

2 Comments

This doesn't address their actual code, though. There should be ways to debug the code without using a specific text editor
Check this post might help if its angular application stackoverflow.com/questions/56585794/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.