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?

2

4 Answers 4

1

This should work

window.location.href

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried see if it works:

document.URL

Comments

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

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.