doesn't work?", "text": "<p>I'm working on a project, and I didn't understand why a call to external script doesn't work.</p>\n\n<p>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?</p>\n\n<p>My browser is a recent Chrome, and my OS is Ubuntu.\nMy HTML file is index.html :</p>\n\n<pre class="prettyprint"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta charset="utf-8"&gt;\n&lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;\n&lt;title&gt;MyPage&lt;/title&gt;\n&lt;/head&gt;\n&lt;body&gt;\n &lt;p&gt;Blablabla&lt;/p&gt;\n &lt;script type="text/javascript" src="/script.js"&gt;&lt;/script&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>The Javascript file is script.js in the same folder:</p>\n\n<pre class="prettyprint"><code>&lt;script type="text/javascript"&gt;\nalert('Hey');\n&lt;/script&gt;\n</code></pre>", "answerCount": 2, "upvoteCount": 500, "dateCreated": "2026-06-28 20:20:06", "dateModified": "2026-06-30 10:45:39", "author": { "type": "Person", "name": "florian" }, "acceptedAnswer": { "@type": "Answer", "text": "<p>Paths starting with <code>/</code> are absolute paths. If the script and the HTML page are in the same directory, the script's path is simply "script.js":</p>\n\n<pre class="prettyprint"><code>&lt;script type="text/javascript" src="script.js"&gt;&lt;/script&gt;\n&lt;!-- Here --------------------------^ --&gt;\n</code></pre>", "upvoteCount": 189, "url": "https://exchangetuts.com/index.php/why-a-simple-script-src-script-doesnt-work-1767991803552350#answer-1782850806198066", "dateCreated": "2026-06-29 20:45:39", "dateModified": "2026-06-30 10:45:39", "author": { "type": "Person", "name": "Mureinik" } }, "suggestedAnswer": [ { "@type": "Answer", "text": "<p>If the file is in the same folder remove the "/" from script.js</p>\n\n<p><code>&lt;script type="text/javascript" src="script.js"&gt;&lt;/script&gt;</code></p>\n\n<p>Also if the js file has the script tags remove them.</p>\n\n<p>If you want the alert when the doc is ready, consider doing something like:</p>\n\n<pre class="prettyprint"><code>document.addEventListener("DOMContentLoaded", function(event) { \n alert('Hey')\n});\n</code></pre>", "upvoteCount": 42, "url": "https://exchangetuts.com/index.php/why-a-simple-script-src-script-doesnt-work-1767991803552350#answer-1782850806204617", "dateCreated": "2026-06-29 20:45:39", "dateModified": "2026-06-30 08:45:39", "author": { "type": "Person", "name": "FutoRicky" } } ] } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 500
florian Avatar asked Jun 28 '26 20:06

florian


2 Answers

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 --------------------------^ -->
like image 189
Mureinik Avatar answered Jun 30 '26 10:06

Mureinik


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')
});
like image 42
FutoRicky Avatar answered Jun 30 '26 08:06

FutoRicky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!