SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing part of a URL to store in a variable.

    I am making a page for work, sorry I can't show it it's confidential. Anyways, the way the site structure is set up, is like this:

    www.server.com/references/
    www.server.com/references/doc1.html

    I have a navigation bar at the top of the page, but I want to change the classes depending on what section the user is on. So basically, if the user is at www.server.com/references/doc32.html, the 'references' graphic at the top would have a different style class applied to it. I can get that part done, I just don't know how to get the URL, and strip it, or match parts of it to store in a variable. If i can get javascript to search and strip away everything but 'references' or 'webforms' that would be perfect.

    This page will be updated by many people, and many of them don't know html let alone anything script wise, so I have been smart and I'm keeping the files external. The idea was to create a page that will run efficiently, and doesn't require the use of tables, etc.

    Anyways, I need a solution to this. The only reason I want this, is so the user will know what "section" they are because the graphic navigation at the top will be highlighted a different color. I just don't want to have to manually change the file, I just want one header file, and a javascript that is external to pull this information for me. The users who update the site will never touch the external files or the header files, etc. All they are worried about is updating the content, so therefor I need to make it where they don't have to ever touch anything else, and I think javascript would be the solution for this.

    Any ideas on how to do this or did I make no sense whatsoever? lol.. sorry if i didn't.

    Thanks in advance fellow web gurus.

  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is probably not the best way, but I came up with it off the top of my head:
    <script language="JavaScript">
    loc=location.href.split("/")
    alert(loc[3])

    </script>

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    if (location.href.match(/\/(\w+)\/[\w\.]+$/) != null)
    	var section = location.href.match(/\/(\w+)\/[\w\.]+$/)[1];
    alert(section);
    ::: certified wild guess :::


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
  •