SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    NYC
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Capturing the URL and then matching it..HELP PLZ

    Hello Everyone,

    I am trying to write a logic that would grab the URL and then write the html/asp file name onto the loaded page. I would highly appreciate your help. This is what I have come up with:

    1. Capture the link
    2. split it into an array
    3. match the file name
    4. write the name

    I am running the (for..in)loop and then setting the switch statement within the for..in loop.

    PHP Code:
    var getURL location.href;
    var 
    my_arr getURL.split("/");

    // Now my_arr is and can be displayed with for in loop

    for (x in my_arr)
    {
      switch (
    x)
      {
      
    home.html
      
    document.write("HOME.HTML");
      break;

      
    service.html:
      
    document.write("SERVICES.HTML"); 
      break;

      
    portfolio.html:
      
    document.write("PORTFOLIO.HTML");
      break;

      default:
      
    document.write("WOW");
      }



  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You shouldn't really use for...in here, since it is designed to be used with object literals. Ideally you could use forEach, but as far as I know only FF1.5 and later support it.

    You should use pop() to get the final part of the array you've made. Then you can just print it, making it uppercase if you want:
    Code:
    var arr = window.location.href.split("/");
    var filename = arr.pop();
    document.write(filename.toUpperCase());
    Doing this with javascript is daft, though, when if you're using ASP you could just do it with that.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •