views:

59

answers:

2

Hi, i need some help please,

i want to show an image on my Page depending of the first directory of the URL.

Example:

In Any of this URLs will show the image1.jpg

  • www.mysite.com/audio/amplifiers/400wats.html
  • www.mysite.com/audio/
  • www.mysite.com/audio/amplifiers/

In any of this others will show the image2.jpg

  • www.mysite.com/video/spots/40wats.html
  • www.mysite.com/video/amplifiers/400wats.html
  • www.mysite.com/video/lighting/laser.html
  • www.mysite.com/video/laser/

At the moment i can show the image only if the url is only the first directory, bu no in the internal directory or documents.

This is the script that i'm using right now:

<script type="text/javascript">
switch (location.pathname) 
{
   case "/audio/":
      document.write("From Web<BR>")
      break 
   case "/video/":
      document.write('<A HREF="slides.htm" target="_blank"><IMG SRC="/adman/banners/joinvip.gif" WIDTH=728 HEIGHT=90 BORDER=0></A>')
      break
   default:
      document.write('<A HREF="http://www.apple.com" target="_blank"><IMG SRC="http://www.amd.com/us-en/assets/content_type/DownloadableAssets/NEW_PIB_728x90.gif" WIDTH=728 HEIGHT=90 BORDER=0></A>')
      break    
}
</script>

Thank you

A: 

You could write

if (/\/audio\//.test(location.pathname))
    document.write("From Web<BR>");
else if (/\/video\//.test(location.pathname))
  document.write('<A HREF="slides.htm" target="_blank"><IMG SRC="/adman/banners/joinvip.gif" WIDTH=728 HEIGHT=90 BORDER=0></A>')
else
  document.write('<A HREF="http://www.apple.com" target="_blank"><IMG SRC="http://www.amd.com/us-en/assets/content_type/DownloadableAssets/NEW_PIB_728x90.gif" WIDTH=728 HEIGHT=90 BORDER=0></A>');
SLaks
this works too, will test both, thank you very much.
Nestorete
A: 

Try this:

var dir = location.pathname.split("/")[1];
if (dir == "audio")
      document.write("From Web<BR>")  
else if (dir == "video")  
      document.write('<A HREF="slides.htm" target="_blank"><IMG SRC="/adman/banners/joinvip.gif" WIDTH=728 HEIGHT=90 BORDER=0></A>')  
else
      document.write('<A HREF="http://www.apple.com" target="_blank"><IMG SRC="http://www.amd.com/us-en/assets/content_type/DownloadableAssets/NEW_PIB_728x90.gif" WIDTH=728 HEIGHT=90 BORDER=0></A>');

This will split the pathname into an array of directories, with the current filename at the end of the array. The first directory will be the first item in the array and is set to the dir variable. Unlike the regex method provided by SLaks (which should also work for you, by the way), this method doesn't use multiple regular expressions (which will degrade performance if there are many) and this method would also work correctly in the unlikely event that the /video/ directory contained an /audio/ sub-directory, whereas this is difficult to achieve with a regex test. Similar to SLaks' answer, I replaced your switch with an if, just because if statements are usually more suited to this kind of thing.

Andy E
Works Fine but..only one correction in this line:var dir = location.pathname.split("/")[0];-->>No zero, works with [1] Thank you
Nestorete
@Nestorete: Ahh you're correct, i forgot about the preceeding slash. It's not present in Windows Gadgets (which I work with mostly), whose URLs look like `x-gadget:///directory/file.htm`. Fixed my code :-)
Andy E