Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    parsing out basename from location.pathname?

    I can do this easily in PHP but how can I get just the filename (with no extension or domain) in Javascript.

    Code:
    alert(location.pathname);
    Will return: "etc/etc/etc/bobs_page.html"

    But I only want "bobs_page" read into my variable. Does javascript have an equivilant to getBaseName?

    Thank you.

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    var path_name=location.pathname;
    var base_name=path_name.lastIndexOf("/")+1;
    alert(path_name.substring(base_name));
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #3
    New to the CF scene
    Join Date
    May 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thank you

    Hey thanks!!
    Exactly what I was looking for; here is how I finished it after looking up 'substring' and 'lastIndexOf' based on your suggestion.

    Code:
    // get instructor name
    var pathName=location.pathname;
    var startLoc=pathName.lastIndexOf("/")+1;
    var endLoc=pathName.lastIndexOf("html")-1;
    var instructorName = pathName.substring(startLoc, endLoc);

  • #4
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can also take out the 'var endLoc' line and just do:

    Code:
    var instructorName=pathName.substring(startLoc, pathName.length-5);
    It does the same thing as the endLoc variable but it basially counts how many characters are in the whole string and then subtracts 5 characters from the end of the string and those 5 characters will be '.html'. Its just a shorter way to do the same thing, just giving you some options.
    I'm only telling you how I would do it, not how its supposed to be done. ;)


  •  

    Posting Permissions

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