Link to home
Start Free TrialLog in
Avatar of esolem
esolem

asked on

Image path in include-file

Hello all.

I have made a html file that works as an header for the rest of my pages.  The first thing I do in a page is to include it like this:
<!--#include virtual="webapp/head.asp" -->

This head.asp contains some images and the images causes some problems for me. The problem is, that I want to use this header in pages that is another directory. Lets say a sub directory called A. The head.asp file the looks for a "images"-directory under the "a" dir. but its not there, because its under the root dir. Is there a way I can tell the img tag where the images is, regardelss of sub-directory?

Thanks all.

Endre
ASKER CERTIFIED SOLUTION
Avatar of ClassyLinks
ClassyLinks
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<!--#include virtual="/webapp/head.asp" -->
Use a paths from web server root directory for your includes (see comment above) and images, like <img src="/images/image.gif">. The leading slash (/) represents a web server root directory.
ahoffmann & aryumin.....that won't work is esolem is putting the include file in many different levels of the web.  If the file hosting the include is in the root directory, the path to the images is very different than if the file is 2 levels down in the structure.  By using absolute URLs, you can put the file anywhere and it will work correctly.
What I did with this was grab the current location of the file you are in. Then I count the amount of the \-sign. Every \ will be replaced by a ../ and be written to the client.

The following function is one I use often when I have a true directory structure:

Public Const ROOTDIR = "RootDirectory"
Public Function CurrentDownPage()
        sTmp = Request.ServerVariables("SCRIPT_NAME")
     iLast = InStrRev(sTmp, ROOTDIR)+len(ROOTDIR)
         
     iCount = ubound(split(mid(sTmp, iLast), "/"))
     sTmp = ""
         
     for i=1 to iCount-1
        sTmp = sTmp & "../"
     next
     currentdownpage = stmp
End Function

replace RootDirectory with the name of the main-directory (the directory in which all elements of your website are in).

regards,
CJ
Interesting approach CJ....does it effect load time in any way?  What I mean is, does the script add to the time it takes to load the entire file or is it insignificant?
It all processes on the server. The function is only a couple of lines, and you can save the returnvalue in a variable and use it each time. When working with a directory structure and having all images at one place this is the fastest way and to still use relative paths. The load time is insignificant, of course depending on the NT server it is being ran on. It's in the miliseconds...

Check out: http://3fm.omroep.nl and click on any of the links at the left side to see the menu in the contents area. All the general functions have been made general and use the above script to know where it is at.

regards,
CJ
Cool!  Good job!
2ClassyLinks:
The path without leading slash (src="images/image.gif") is relative to current directory, but the path with leading slash (src="/images/image.gif") is a absolute path from webserver root.
So, the URL
http://www.server.com/images/image.gif
and
/images/image.gif
are identical in this context (the html page and image was placed on the same server).

And we can use the same syntax for include virtual too.
Doesn't work on my server aryumin....just tried it.  Won't even work from the root never mind a buried page.


For my test I created a page in the root of my web....then I put in a pic who's correct URL is rootdirectory/_sitepages/pictures/cat5.jpg

If I put in: src="/_sitepages/pictures/cat5.jpg" it cannot find the image
I think aryumin means to set the base of the image paths

<base path="http://www.myserver.com"> or something like that.
2ClassyLinks: Can you access your image via
http://www.your_server.com/_sitepages/pictures/cat5.jpg?
Check the source code of _this_ page (src of scripts, href's, images), for example....

2CJ_S: It's not needed....
yes.  by using the absolute url I can get the image.
2ClassyLinks: Can you post an URL here?
Interesting....server dependant?....aryumin's technique works on my Win2000 web server, but not on my Win98 platform.
> Doesn't work on my server aryumin....just tried it.
ClassyLinks, then please check your server configuration.
Avatar of esolem
esolem

ASKER

Thanks all for lots of input here.
Both methods work fine on my server. it wat the / soulution I was looking for, but couldnt figure out.

becaus of the wast amout of point I've got I'll give you both (ClassyLink and ahoffmann? points, sorry aryumin, but ahoffmann was first.

CJ: I like the idee, but it's not what I was looking for right now...

ahoffman: get your points by answering the soon to be created points for ahoffmann question.


Thanks for the A....I learned something here today too  8-)