SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru DenverDave's Avatar
    Join Date
    Feb 2001
    Location
    Denver, Colorado
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript equivalent to basename ($PHP_SELF);

    What is the best way to get just the script name that is running without the path and parameter info? This would be the JavaScript equivalent to the PHP command:
    basename ($PHP_SELF);

    Thanks.

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    document.url

    Have fun!

    --Vinnie

  3. #3
    SitePoint Guru DenverDave's Avatar
    Join Date
    Feb 2001
    Location
    Denver, Colorado
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but document.URL gives the entire url, I want just the file name portion. Is there a property or an easy way to split out myfile.html from

    http://test.com/folder/myfile.html?test&test2=1*test3=2

    ? Has to be an easy way !

    Following is a description of each part of the URL diagram:

    protocol represents the beginning of the URL, up to and including the first colon.
    host represents the host and domain name, or IP address, of a network host.
    port represents the communications port that the server uses for communications.
    pathname represents the url-path portion of the URL.
    hash represents an anchor name fragment in the URL, including the hash mark (#). This property applies to http URLs only.
    search represents any query information in the URL, including the question mark (?). This property applies to http URLs only. The search string contains variable and value pairs; each pair is separated by an ampersand (&).

    The above is very nice, but nowhere the actual file name without the path. Dave

    Thanks.
    Last edited by DenverDave; Dec 4, 2002 at 16:29.

  4. #4
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey guys,

    Firstly, document.url is deprecated. I'm sure it still returns the correct value, but just FYI, use the window.location object. On that note, you can get just the filename and all that good stuff...just requires a little work.

    Code:
    var splitAt=location.href.lastIndexOf('\\') > location.href.lastIndexOf('/') ? location.href.lastIndexOf('\\') : location.href.lastIndexOf('/');
    var filename= location.href.substr(splitAt+1,location.href.length);
    aDog

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use location.pathname to make the code a little easier. With this code, you can also set a default page to return in case the href ends with a folder name.

    Code:
    baseName = window.location.pathname.substring(window.location.pathname.lastIndexOf("/")+1) || "index.html";
    travis

  6. #6
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhh, nice pdlob...guess I didn't know about the pathname property...guess I should've looked up the location object in a reference first

    aDog


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
  •