Relative path for a included file - ASP/HTML
-
26-05-2021 - |
Question
Sorry if this question is answered somewhere else but I tried searching several pages and was unsuccessful.
So i have an include file (sidebar) which i am using in all pages.
Default.asp
Products.asp
Salary/Survey.asp
inc/sidebar.asp (this is the included file)
now inside sidebar.asp
I have a link for Salary/Survey.asp
from all other pages at root level, i can simply use href='Salary/Survey.asp'
and will work fine. but when I am on page Survey.asp
, writing href='Salary/Survey.asp'
will become actually Salary/Salary/Survey.asp
. I understand it has to be ../Salary/Survey.asp
to be used properly but it will then not work for root level pages.
I can not use root relative
which is /Default.asp
and /Salary/Survey.asp
as I am working for someone else' project and i dont know his directory structure and thus i only have option to document relative
path.
Hope this is clear to understand and someone helps me out.
Thanks!
Solution
We solved this problem the following way...
- Each of our asp pages included a special file that Dims and sets golbal variables. We called ours Info.asp
- Inside Info.asp we defined a variable called strRelativePath
Dim strRelativePath
strRelativePath = "" - Every asp page set the relative path according to it relative position:
for example:
- Root pages - strRelativePath = ""
- One level deep pages - strRelativePath = "../"
- Two levels deep pages - strRelativePath = "../../"
Then it was a matter of prefacing all the links requiring a relative path with <%=strRelativePath%>
OTHER TIPS
you need to get write this after the that - Salary/Survey.asp
You can get the virtual path to the file from one of several server variables - try either:
Request.ServerVariables("PATH_INFO")
Request.ServerVariables("SCRIPT_NAME")
Either server variable will give you the virtual path including any sub-directories and the file name - given your example, you'll get /virtual_directory/subdirectory/file.asp
. If you just want the virtual directory, you'll need to strip off everything after the second forward slash using whatever method you prefer for plucking a directory out of a path, such as:
s = Request.ServerVariables("SCRIPT_NAME")
i = InStr(2, s, "/")
If i > 0 Then
s = Left(s, i - 1)
End If
or:
s = "/" & Split(Request.ServerVariables("SCRIPT_NAME"), "/")(1)
basically, if your sidebar can be included from programs in different folders, the only 'easy' way is to use absolute paths like you mentioned.
You say can't use it, so I would think of different ways...
- virtual folders: In IIS you could set a virtual folder in salary folder for 'salary' and point it to the site's root.
- OS links (similar to above, but at the OS level)
- use mappath. You could check mappath to see the actual folder you're in, and use the correct include (with/without /salary) though I'm thinking this might give you an error, not sure.