Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Obtaining the relative path with image.src

I need to know how I can obtain the relative path when using .src with Javascript.

In this code target.src evaluates to "http://localhost/images.collapse-gray.GIF" which is no code for me to hardcode, because when I move it to the production server "localhost" won't work:

//Will toggle the image between collapsed and expanded
function ToggleCollExpImage( targetId ){
if (document.getElementById){
target = document.getElementById( targetId );
if (target.src == "images/collapse-gray.GIF"){
target.src = "images/expand-blue.GIF";
} else {
target.src = "images/collapse-gray.GIF";
}
}
}

<img src="./images/collapse-gray.GIF" id="imgDemographicsCollapseExpand" height="14" width="14" onclick="ToggleDisplay('tblDemographics');ToggleCollExpImage('imgDemographicsCollapseExpand');" />
[962 byte] By [Billkamm] at [2007-11-11 8:00:02]
# 1 Re: Obtaining the relative path with image.src
Try referencing by the images object.
document.images[0].src and see if it returns the relative path. It should return just the string it finds there. The [0] assumes it's the 1st img tag on the document. If it isn't change that number accordingly.
JPnyc at 2007-11-11 23:34:55 >