-2

I am very much confused with these four categories while linking js, css and images to the application as I am creating folder structure for my application ..

I heard ~/Images/up.png means wherever you are it comes to the root and searches for Image folder and gives the image. When I tried this .., it doesn't work. I used ../Images/up.png and it works .. I used /Images/Images.png and it also worked so please explain me why.

2 Answers 2

0

You primarily use absolute paths when accessing things outside your own immediate domain.
So if your site is hosted on www.mysite.com and your images are located on images.mysite.com - you'll use absolute paths for them.

Likewise, you use relative paths internally. Relative paths are the ../images/up.png. You can use absolute paths internally as well, but often you don't want to do it, because it eases usage to not have to think about which domain the resource is located on, but only where relative to your content.

As for the ~/ then it is a .NET thing that indeed means back to the root of the site. However it requires to be used specially so you cannot just make a normal link and use ~/
It needs to be resolved server side which for example can be done using ResolveUrl - for example ResolveUrl("~/images/up.png") and use the returned as the link. Specific controls will resolve the url for you automatically, but it's still resolved via .NET.
So it's primarily used because you do not have to worry about how many levels the page your working on is located from the root (so you don't have to worry about whether it's currently ../images/up.png or ../../images/up.png)

0

~ is the root

so '~/js/fancyscript.js' means go back to root go into js and find fancyscript.js

'/js/fancyscript.js' means go into js and find fancyscript.js this will not always work since there is not always an /js folder

for example this is my project

site
    /css
        style.css
    /js
        fancyscript.js
    /include
        view.html
    index.html

I have my index in my root folder from here i can use the relative path to include my fancy script '/js/fancyscript.js'

It will find /js and it will find my script

But when i'm working in /include/view.html i have to go back 1 folder first so the relative path would be in that case '../js/fancyscript.js' (up one folder go into /js and find fancyscript.js)

since the js folder is in the root in my example i can also use '~/js/fancyscript.js'

The difference between an absolute path and a relative path is that an absolute path is a complete path and a relative is from the point where the code is being executed

so the example shown above are relative paths and an axample for an absulote path would be

http://www.google.com

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.