2

I'm linking an external javascript file. This always works if I want to go back a directory:

<script src="../../scripts/myScript.js" type="text/javascript"></script>

But I mistyped how many directories back I wanted to go, making three ../ as opposed to two. But it still worked. I even tried with six or seven ../ and the external javascript still loaded correctly. I put an alert in the file to see if it were being cached or something. The alert fired. Chrome, Firefox, IE8 all found the file. Does the browser just go back until it finds a directory with the filename or directory name you've specified after the last ../ and then proceed forward again?

1252748
  • 14,597
  • 32
  • 109
  • 229
  • 3
    It can't go above root level, thats what I think. So if the script is in `/scripts/myScript.js` then you can add as many `../` as you want (at least two then) – jtheman Jun 24 '13 at 18:41
  • aha, that makes good sense. Thanks! I think that's the answer. – 1252748 Jun 24 '13 at 18:46
  • 1
    You know, if you're using PHP, then you could use something like the method found at an Answer [here](http://stackoverflow.com/questions/2820723/how-to-get-base-url-with-php#answer-2820771) and then simply place in ` – SpYk3HH Jun 24 '13 at 18:51
  • @SpYk3HH That's really cool. I hadn't ever thought about doing it that way. Thanks! I guess I'll spend the day redoing a million links! heheh – 1252748 Jun 24 '13 at 19:05
  • not a problem. it saves a lot of future headaches to get into the habit of doing it that way. If you're using a platform like Codeigniter, it's even easier, since they already have built in helper methods of `base_url` and `site_url`. I think even Word Press has something similar. If you wan't something similar for JavaScript, I have a jQuery plugin [**here**](http://jsfiddle.net/SpYk3/2ZuTe/) – SpYk3HH Jun 24 '13 at 19:15

1 Answers1

3

The browser can never reach above root level, so unregarded of how many ../ you put before the directory name you end up at the webroot level / then goes down again.

If your script resides in the folder /scripts/myScript.js then you could either use the absolute path for the script or add as many ../ as you want before the path to the script as long as you reach up to the root level.

jtheman
  • 7,421
  • 3
  • 28
  • 39