Advanced Search

Results 1 to 7 of 7

Thread: A script to return the page name

  1. #1
    Join Date
    Jul 2005
    Posts
    101
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default A script to return the page name

    I'd have thought this was eays but cannot find a current document.name property which would return say "index.htm".

    I have a js generated nav bar which is included on all pages and I want to highlight and remove the anchor for the Home option when the Home page is the one being viewed.

    I could parse window.location.href but is there an 'easier' property to use.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,683
    Thanks
    43
    Thanked 3,128 Times in 3,094 Posts
    Blog Entries
    12

    Default

    Not that I know of but, parsing is hardly required, a simple index query will do:
    Code:
    if (location.href.indexOf('index.html')!==-1){
    stuff to do if it is index.html goes here
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2005
    Posts
    101
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thanks, John.

    I would term Using 'indexof ' as parsing in that the string has to be examined.

    It does seem a bit odd that js does not have a built-in function to return page name.

    Billy

  4. #4
    Join Date
    Jul 2005
    Posts
    101
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Just one other question on this.

    If I assign var path = window.location.href in a script on the page, do I have to reassign if I have a second script which also needs to get the path? If so, do I need to use a different name for the var.

    Thanks

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,683
    Thanks
    43
    Thanked 3,128 Times in 3,094 Posts
    Blog Entries
    12

    Default

    To the first point, parsing means teasing out a string into its component parts. I guess we are half way there with indexOf(). All it does (in this case) is see if the substring is contained in the main string.

    To your question, if the variable is set outside of a function (or declared as a global variable within a function), it becomes a global variable for the page, and thus available to all scripts that follow it logically* on the page. If there is any doubt, it is best to test it by temporarily placing an alert at the spot in your code where you want the variable to be available and see what it returns:
    Code:
    alert(path)
    *NOTE: Just because code appears after other code on a page doesn't always mean that it executes after the preceding code.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jul 2005
    Posts
    101
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thanks again, John

  7. #7
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by wkenny
    It does seem a bit odd that js does not have a built-in function to return page name.
    It's not the responsibility of the scripting language to provide this sort of feature, but the object model in which it operates. However, given that it's so easy to obtain the value, there really isn't any point for browser vendors to provide it.

    Code:
    function getFileName() {
      var url = document.URL,
          i = url.lastIndexOf('/') + 1,
          j = url.indexOf('#', i),
          k = url.indexOf('?', i);
    
      if(-1 == j) {j = url.length;}
      if(-1 == k) {k = url.length;}
    
      return url.substring(i, Math.min(j, k));
    }
    Note that this will return an empty string when the document was obtained in response to a path-only URL. That is, a URL like

      http://www.example.com/

    will return an empty string. The browser has no idea what file is used when returning the HTTP response - that's entirely down to the server and is dependent upon its configuration.

    Mike

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
  •