PDA

View Full Version : Pathname to graphics



kbdci
11-07-2005, 03:51 PM
SwitchContent.htm
http://www.dynamicdrive.com/dynamicindex17/switchcontent.htm

I have been trying to set up SwitchContent.htm so that all of the code in the Head section is located in a folder in a JavaScript file named “expand.js” so that I can call it from any page in any folder in my web. This works fine because the call I use expresses the location of the JavaScript file as a relative address and FrontPage (which I am using to build my site) modifies that relative address depending on the location of the calling page.
The images, “expand.gif” and “contract.gif” are in the same directory as the “expand.js” file. Is there a way that I can call them from the JavaScript that will work no matter where the calling page is located? If I don’t use a pathname and assume that the graphics files are in the same place as the “expand.js”, the graphics don’t appear. When I use a specific pathname on my disk-based web, everything is fine. But then the code doesn’t work on my real http site. If I use a specific pathname to my http site, the code works there.
I would like to determine whether my JavaScript code is running on my local drive in a disk-based web or on my http site in an IIS-based web. Is there a command that can tell my JavaScript code where my site is currently running? I am hoping that I can then use an “if” statement to determine this and change the pathname to find the correct location of graphics accordingly.
If there is an easier way, please let me know. I am just starting to work with JavaScript and need some help.
Thanks.
Kathy

jscheuer1
11-07-2005, 05:31 PM
Assuming that the directory structure is the same on the server as it is on the local machine, it would be better to just use the correct relative path to the image files and the script in the html files. In the javascript file, if used by multiple html files in different directories, an absolute path should be used. Generally one copy is made for local use, one for online.

If you want to though, you can use this test:


if (location.href.indexOf('http')!==-1){
code here for online
}
else {
code here for local
}