Reputation: 13
I am coding a PHP website homepage using a MAMP localhost server. I would like to connect the main HTML file to a javascript file, but when I do so, none of the javascript is executed, and an error that says Failed to load resource: the server responded with a status of 404 (Not Found) appears.
I have tried using the absolute file path when including the file under the src attribute (though the js file is in the same folder as the main html file anyway). I tried turning the server on and off and reloading the page several times, but the error still appears.
<head>
<script type="text/javascript" src="/homescript.js"></script>
</head>
<body onresize="changeHeaderDisplay()" onload="responsiveCarousel()">
</body>
Upvotes: 1
Views: 786
Reputation: 12218
The forward slash at the beginning of the filename in the script tag is the problem :
src="/homescript.js"
When your browser sees a forward slash, it assumes the file is at the top level of the domain it's searching. So if your html file is at:
file:///Users/Jack/test_program/index.html
it is searching for homescript.js at:
file:///homescript.js
To make it search for it at:
file:///Users/Jack/test_program/homescript.js
simply remove the leading forward slash:
<script type="text/javascript" src="homescript.js"></script>
Edit: On a website, it's the same phenomenon:
With leading slash:
http://www.example.com/homescript.js
Without leading slash: http://www.example.com/test_program/files/homescript.js
Upvotes: 1