www.webdeveloper.com
Results 1 to 4 of 4

Thread: Way to read current URL?

Hybrid View

  1. #1
    Join Date
    Apr 2010
    Location
    Brooklyn NY
    Posts
    104

    Way to read current URL?

    so I'm interested in knowing if there's a way through javascript, php whatever to be able to read the pages current URL. For example when the page loads and knows the difference between www.mySite.com/ and www.mySite.com/#about

    I'm trying to find if there's a way to use the '#about' as a variable for a particular script instead of just it being dummy text on the end of the URL.

  2. #2
    Join Date
    Nov 2010
    Posts
    86
    You can use the document.URL property to read the entire url. Then you can use whatever string or regular-expression routines are appropriate to extract the part you want.

    Here is a basic example of reading two variables out of the URL.

    http://webinterfacetricks.com/urltes...llo&two=banana

    There are lots of comments in the source code to explain each step but please let me know if anything is unclear.

  3. #3
    Join Date
    Apr 2010
    Location
    Brooklyn NY
    Posts
    104
    Just what I was looking for, thanks.

  4. #4
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Code:
    function getURLandNVP() {
        var thisURL = document.URL;
        var arrayURL = new Array();
            arrayURL = thisURL.split("/");
        var arrayLen = arrayURL.length;
        var thisDelim = "/";
        var thisPath = "";
        var thisNVP = "";
        var qs = false;
        var nvp, thisPart, pre, sp;
        for(i=3; i<arrayLen; i++) {
            thisPart = arrayURL[i];
            thisPartLen = thisPart.length;
            nvp = thisPart.indexOf("?");
            sp = thisPart.indexOf(".");
            if((nvp == -1) && (!qs)) { // If NO URL parameters, add to path
                thisPath += thisDelim + thisPart;
                }
            else if((nvp > -1) && (!qs)) { // If URL parameters, get path and separate from URL parameters
                pre = thisPart.substring(0,nvp);
                thisPath += thisDelim + pre;
                thisNVP = thisPart.substring(nvp+1,thisPartLen);
                thisNVP = thisNVP.replace("&","\n","all");
                }
            else if((sp > -1) && (!qs)) { // If using slash delimiter after document name
                qs = true;
                thisPath += thisDelim + thisPart;
                }
            else if(qs) {
                thisNVP += "\n" + thisPart;
                }
            }
        var thisAlert = thisPath; if(thisNVP.length != 0) { thisAlert += "\nwith NVP of\n" + unescape(thisNVP); }
        alert(thisAlert);
        }
    |||||
    o . Q
    ___ "You live and you learn; or you don't live long." - Lazarus Long

    ^_^

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles