SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot markarena's Avatar
    Join Date
    May 2003
    Location
    Perth, Australia
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Document location during runtime

    I'd like to find the document location during runtime for a script I'm using, and having next to no experience with JavaScript, I don't know how.

    For example, if my document is local:
    http://localhost/websites/example/script.js

    If my site is live:
    http://www.example.com/script.js

    How can I extract everything but the script name and put it in a variable?

  2. #2
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not specifically what you asked for, but would any of these solve your problem?

    location.href
    location.protocol
    location.hostname
    location.host
    location.port
    location.pathname
    location.search
    location.hash
    location.pathname.substring(0,location.pathname.lastIndexOf('/'))

    Demo


    Andy
    From the English nation to a US location.

  3. #3
    SitePoint Zealot markarena's Avatar
    Join Date
    May 2003
    Location
    Perth, Australia
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is fantastic stuff. Thanks! I ended up using this...

    var rootdir = location.protocol + '//' + location.hostname + location.pathname.substring(0,location.pathname.lastIndexOf('/')) + '/';

    What I'm really wondering though, is why this isn't a variable built into the language. I'm doing this so I don't have to change the root directory on my localhost then to the domain I upload to...

    Anyway, this works fine!


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
  •