0

I have a website with index.html in the root folder, and a folder named apples. Inside apples I have another index.html. Currently I am attempting to use AngularJS to load the same navbar into both pages. However, when I tried linking to the Javascript file like so: <script src="/controller.js">, the JS file loads for the index.html in the root folder, but it refuses to load the file for the index.html in the apples folder. Does anyone know why? In the apples folder, it keeps linking to apples//controller.js.

Neil A.
  • 841
  • 7
  • 23
  • done any basic debugging, like checking server access logs to see what happens to your request? Maybe you've got a misbehaving rewrite that's preventing access to the js file and sending the request off to the homepage. – Marc B Aug 18 '16 at 15:11
  • you should go one level up. So for index.html inside apples folder, you need to do `src="../controller.js"` – Kaushal Niraula Aug 18 '16 at 15:16
  • @KaushalNiraula: I want to copy and paste this line into any and all HTML pages I have, no matter how many folders it is inside (since it loads the same header for all of them). – Neil A. Aug 18 '16 at 22:50

2 Answers2

2

Are you running this on a server or just on your desktop? The path /controller.js should work if you're running from a web server but likely won't work if you're calling it locally.

Jon Ewing
  • 392
  • 1
  • 4
  • 13
  • 2
    I used `http-server` (on mac) and that resolved the issue. I didn't realize that running it from a server made that difference. Do you know why? And thanks for the help. – Neil A. Aug 18 '16 at 22:45
  • /controller.js is a valid absolute path on a server because the server knows you're referencing the root folder - /wwwroot/controller.js or /public_html/controller.js - but if you try to run the same code locally in a web browser, it's not a valid path on your desktop, so you need to use relative paths or, better still, have a web server running on your development machine. – Jon Ewing Aug 19 '16 at 09:12
0
<script src="../js/controller.js"></script>
          __ ______ ________
          |    |       |
          |    |       |___ 3. Get the file named "logo.png"
          |    |
          |    |___ 2. Go inside "images/" subdirectory
          | 
          | 
          |____ 1. Go one level up

Reference

Community
  • 1
  • 1
Smit
  • 2,078
  • 2
  • 17
  • 40
  • This is a good answer, but I want to be able to copy and paste this one line of code into _any_ HTML page I have, no matter how many folders it is inside (it loads the same header for all of them). That's why I prefer the `/` method over the `../` method. – Neil A. Aug 18 '16 at 22:49