1252748
1252748

Reputation: 15369

Javascript finds external file even with extra ../

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?

Upvotes: 2

Views: 139

Answers (1)

jtheman
jtheman

Reputation: 7491

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.

Upvotes: 3

Related Questions