0

I want to save an entire URL paths to a variable, including the php vars, eg:

mysite.com/pagename?id=2

I can use

var pathname = window.location.pathname;

but this only retrieves the URL without the variables.

Is there a function to retrieve the URL as a literal string?

MeltingDog
  • 12,714
  • 38
  • 143
  • 270
  • it's hard. this answer explains how to get the query parameters in javascript http://stackoverflow.com/questions/979975/how-to-get-the-value-from-url-parameter – roo2 Dec 02 '13 at 06:18
  • store this link in your bag of tricks [parsing-urls-with-the-dom](http://james.padolsey.com/javascript/parsing-urls-with-the-dom/) – charlietfl Dec 02 '13 at 06:26

4 Answers4

1

This should work

window.location.href

sravis
  • 3,416
  • 6
  • 28
  • 69
0

Have you tried see if it works:

document.URL
Ankit Tyagi
  • 2,341
  • 9
  • 19
0

Can you try this,

//  Get current page url using JavaScript
var currentPageUrl = "";
if (typeof this.href === "undefined") {
    currentPageUrl = document.location.toString().toLowerCase();
}
else {
    currentPageUrl = this.href.toString().toLowerCase();
}

Ref: http://www.codeproject.com/Tips/498368/Get-current-page-URL-using-JavaScript

Krish R
  • 22,188
  • 7
  • 49
  • 57
0

It's hard , this answer explains how to implement it from the top response:

function getQueryParams(qs) {
    qs = qs.split("+").join(" ");

    var params = {}, tokens,
        re = /[?&]?([^=]+)=([^&]*)/g;

    while (tokens = re.exec(qs)) {
        params[decodeURIComponent(tokens[1])]
            = decodeURIComponent(tokens[2]);
    }

    return params;
}

//var query = getQueryParams(document.location.search);
//alert(query.foo);
Community
  • 1
  • 1
roo2
  • 5,781
  • 2
  • 29
  • 45