Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

obtaining the relative path from image.src 1

Status
Not open for further replies.

Billkamm

Programmer
Joined
Feb 9, 2006
Messages
74
Location
US
I need to know how I can obtain the relative path when using .src with Javascript.

In this code target.src evaluates to " 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";
}
}
}
 
This is from view source in MSIE of my rendered web page:

<img src="./images/collapse-gray.GIF" id="imgDemographicsCollapseExpand" height="14" width="14" onclick="ToggleCollExpImage('imgDemographicsCollapseExpand');" />
 
Why not use "indexOf", instead? That way, you just search for the relative path and filename, rather than looking for an exact match:

Code:
if (target.src.indexOf('/images/collapse-gray.GIF') != -1) {
   // this will run if '/images/collapse-gray.GIF' appears anywhere in the image path + filename
} else {
   // this will run if it does not.
}

You might want to make sure you are using the correct case for your filenames, too. The indexOf method is case-sensitive, so make sure your GIF images do have an uppercase extension, or change your string to lowercase.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
OK... just for fun... here is another variation:
Code:
// define the names to give to each image
var collapseImage = 'collapse-gray.gif';
var expandImage = 'expand-blue.gif';

// define the path to the images
var pathToImages = '/images/';

// toggle the image between collapsed and expanded
function ToggleCollExpImage(targetId) {
  if (document.getElementById) {
    target = document.getElementById(targetId);
    if (target.src.indexOf(collapseImage) > -1) {
      target.src = pathToImages + expandImage;
    } else {
      target.src = pathToImages + collapseImage;
    }
  }
}

And if you truely wanted to have fun... you'd write it so it's hard for anyone else to be able to understand it (although to do that properly, you might not use such practical variable names)...
Code:
// toggle the image between collapsed and expanded
function ToggleCollExpImage(targetId) {
  if (document.getElementById) document.getElementById(targetId).src = pathToImages + (document.getElementById(targetId).src.indexOf(collapseImage) > -1) ? expandImage : collapseImage;
}
[smile]
Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top