Specify a Root Path of your HTML directory for script links?

HtmlPath

Html Problem Overview


I'm writing a template for dreamweaver, and don't want to change the scripts for subfolder pages.

Is there a way to make the path relative to the root directory?

for example:

<link type="text/css" rel="stylesheet" href="**root**/style.css" />

Instead of **root** above, I want a default path there. Is there any way to do anything like this?

Html Solutions


Solution 1 - Html

To be relative to the root directory, just start the URI with a /

<link type="text/css" rel="stylesheet" href="/style.css" />
<script src="/script.js" type="text/javascript"></script>

Solution 2 - Html

I recommend using the HTML <base> element:

<head>
    <base href="http://www.example.com/default/">
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
</head>

In this example, the stylesheet is located in http://www.example.com/default/style.css, the script in http://www.example.com/default/script.js. The advantage of <base> over / is that it is more flexible. Your whole website can be located in a subdirectory of a domain, and you can easily alter the default directory of your website.

Solution 3 - Html

/ means the root of the current drive;

./ means the current directory;

../ means the parent of the current directory.

Solution 4 - Html

Just start it with a slash? This means root. As long as you're testing on a web server (e.g. localhost) and not a file system (e.g. C:) then that should be all you need to do.

Solution 5 - Html

This is oddly confusing to me. I know it shouldn't be. To check my understanding, I'd like to use a family relations model to compare. Assuming "You" is the current webpage, is the following correct?

<img src="picture.jpg"> 	       In your folder with you, like a sibling
<img src="images/picture.jpg"> 	   In your child's folder, under you
<img src="../picture.jpg"> 	       In your parent's folder, above you
<img src="/images/picture.jpg">    In your cousin's folder

So, up to parent, over to sibling, down to their child = your cousin, named "images".

Solution 6 - Html

As Alexander Jank mentioned <base href="http://www.example.com/default/"> is great. When using sub-domains e.g. default.example.com base works great, because the JS and CSS loads from the said sub-domain and is accessible to both default.example.com and example.com/default

When using the root path, and your JS and CSS files are located in example.com/css, or example.com/js, then the subdomain has no access and the root of the subdomain is not accessible, except using the base.

Solution 7 - Html

Use two periods before /, example:

../style.css

Solution 8 - Html

You can use ResolveUrl

<link type="text/css" rel="stylesheet" href="<%=Page.ResolveUrl("~/Content/table-sorter.css")%>" />

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGazowView Question on Stackoverflow
Solution 1 - HtmlQuentinView Answer on Stackoverflow
Solution 2 - HtmlAlexander JankView Answer on Stackoverflow
Solution 3 - HtmlMistyDawnView Answer on Stackoverflow
Solution 4 - HtmlLee KowalkowskiView Answer on Stackoverflow
Solution 5 - HtmlTriangleGMView Answer on Stackoverflow
Solution 6 - HtmlHendrik van der MerweView Answer on Stackoverflow
Solution 7 - HtmlAgnostikguyView Answer on Stackoverflow
Solution 8 - HtmlAhmed KhajaView Answer on Stackoverflow