Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would a developer place a forward slash at the start of each relative path?

I am examining some code for a friend, and have found that the developer who built his site began each and every relative src, href, and include with a forward slash /.

For example:

src="/assets/js/jquery.js" 

I have never seen this before. So my question is, why would a developer place a forward slash / at the start of a relative path?

like image 804
stefmikhail Avatar asked Sep 30 '11 16:09

stefmikhail


People also ask

What does forward slash mean in file path?

Forward Slash vs Backslash File Path While in Mac, Linux, Android, Chrome, and Steam, all Unix-like operating systems, directories in file paths are separated by forward slashes. For instance, /System/Library/Screen Savers.

Why does HTML use forward slash?

In most cases, the tag is repeated at the end of the content, with the tag name preceded by a forward slash (e.g. </p>), to indicate that the action is no longer needed.

What does forward slash mean in command line?

The forward slash (or simply slash) character (/) is the divide symbol in programming and on calculator keyboards. For example, 10 / 7 means 10 divided by 7. The slash is also often used in command line syntax to indicate a switch.

What is meaning of forward slash in Linux?

In Linux and other Unix-like operating systems, a forward slash is used to represent the root directory, which is the directory that is at the top of the directory hierarchy and that contains all other directories and files on the system.


2 Answers

It's done in order to root the path (making it an absolute path).

It ensures that the path is not relative but read from the root of the site.

This allows one to move a file around and not have to change the links to the different resources.

Using your example:

src="/assets/js/jquery.js" 

If the referencing file is in /pages/admin/main.html (for example) using relative paths you would use:

src="../../assets/js/jquery.js" 

Suppose you move the file to a child directory. No changes would be needed for with the original rooted path, but the relative one would need to change to:

src="../../../assets/js/jquery.js" 
like image 73
Oded Avatar answered Oct 13 '22 23:10

Oded


Adding on @Oded's answer, the slash makes the URL absolute.

For example:

/foo/bar/baz.css 

This translates to:

http://www.example.com/foo/bar/baz.css 

But without the slash, things become a bit different:

foo/bar/baz.css 

This tells the browser to look in the current folder (not the root folder) for the directory foo and then the subsequent directories and the file.


Also, take for instance this HTML:

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

If you move the HTML file into another folder, then the script will not load, as foo.js isn't being moved with the HTML file.

But if you use an absolute URL:

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

Then the JS file is loaded EXACTLY from http://www.example.com/foo.js no matter where the HTML file is.

like image 24
Blender Avatar answered Oct 13 '22 22:10

Blender