Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the whole URL with parameters?

Tags:

javascript

I need to retrieve the whole URL with the parameters as a string. For example, I need to retrieve the following URL:

http://www.keytometals.com/page.aspx?ID=CheckArticle&site=kts&LN=EN&NM=349

I've tried to use:

document.location.href,
document.URL,
window.location.href

but it retrieves just a part of URL:

http://www.keytometals.com/page.aspx

How to get the string containing the current URL as well as its parameters?

One update: I've used the

window.content.document.location.href

and I've got the following URL:

http://www.keytometals.com/page.aspx?ID=CheckArticle

Unfortunately, it is still the part of URL. Can somebody help me how to get the whole URL as a string?

Thanks!

like image 826
sonjafon Avatar asked Jun 25 '11 01:06

sonjafon


People also ask

How do I find the whole URL of a website?

Just right-click in Chrome's address bar select “Always show full URLs” to make Chrome show full URLs. Chrome will now always show the full URL of every web address you open. To disable this feature, right-click in the address bar again and uncheck it.

How can I get parameters from a URL string?

The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions. Note: Page URL and the parameters are separated by the ? character. parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it.


2 Answers

You should use window.location.href for the entire URL (including the query string).

Take a look at Mozilla's documentation for more information and other properties.

Here is another good StackOverflow post on how to Parse query string in JavaScript.

like image 125
vcsjones Avatar answered Oct 06 '22 09:10

vcsjones


You need just plain document.location -- that will include everything. You'll need to write your own code to split it at the ? to get just the query string, and then split that into name/value pairs at each &.

EDIT:

Actually, you can use location.search as well. Here's a snippet I wrote for this:

function loadQueryString(){ 
    var parameters = {}; 
    var searchString = location.search.substr(1); 
    var pairs = searchString.split("&"); 
    var parts;
    for(i = 0; i < pairs.length; i++){
        parts = pairs[i].split("=");
        var name = parts[0];
        var data = decodeURI(parts[1]);
        parameters[name] = data;
    }
    return parameters;
}

params = loadQueryString();
like image 43
Will Martin Avatar answered Oct 06 '22 07:10

Will Martin