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!
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.
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With