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
  • 14,310
  • 43
  • 165
  • 295
  • 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 – actual_kangaroo 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,562
  • 6
  • 36
  • 73
0

Have you tried see if it works:

document.URL
Ankit Tyagi
  • 2,381
  • 10
  • 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,583
  • 7
  • 50
  • 59
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
actual_kangaroo
  • 5,971
  • 2
  • 31
  • 45