Search code examples
javascripthtmlalertsrc

Why a simple <script src="..."> </script> doesn't work?


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>

Solution

  • 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 --------------------------^ -->