11

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!

sonjafon
  • 353
  • 2
  • 8
  • 17

3 Answers3

4

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();
Crozin
  • 41,538
  • 12
  • 84
  • 134
Will Martin
  • 3,874
  • 1
  • 22
  • 37
  • 1
    Why the `eval()` ? Bracket notation is you friend :) – alex Jun 25 '11 at 01:42
  • @alex: Why didn't you fix it by yourself? :) – Crozin Jun 25 '11 at 01:46
  • Hmm. Well, I guess when I originally wrote it, I hadn't fully grasped the fact that arrays and objects are essentially identical in JS. So yeah, bracket works just fine. – Will Martin Jun 25 '11 at 01:46
  • 1
    @Crozin: I don't know if modifying other people's code in answers is what I should be doing. :P – alex Jun 25 '11 at 01:51
  • Can you, please, tell me how to write this params variable as a string? I've tried the document.write(params.toString), but it doesn't work. – sonjafon Jun 25 '11 at 23:44
  • @sonjafon Because `params` is an object literal, it doesn't have a default toString() method. What are you trying to do? It would help us to answer your questions if we knew what the end goal is. Are you trying to write the complete URL into the document to show the end user? Or something? – Will Martin Jun 26 '11 at 04:49
4

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.

Community
  • 1
  • 1
vcsjones
  • 128,004
  • 28
  • 283
  • 274
  • Unfortunately, It doesn't work. When I try to retrieve the URL by using document.write(window.location.href) in http://www.w3schools.com/js/tryit.asp?filename=tryjs_write for example, I've got just the http://www.w3schools.com/js/tryit.asp . Just to mention that I use the Mozillla Firefox 4. – sonjafon Jun 25 '11 at 23:33
0
function getQueryString() {
  var key = false, res = {}, itm = null;
  // get the query string without the ?
  var qs = location.search.substring(1);
  // check for the key as an argument
  if (arguments.length > 0 && arguments[0].length > 1)
    key = arguments[0];
  // make a regex pattern to grab key/value
  var pattern = /([^&=]+)=([^&]*)/g;
  // loop the items in the query string, either
  // find a match to the argument, or build an object
  // with key/value pairs
  while (itm = pattern.exec(qs)) {
    if (key !== false && decodeURIComponent(itm[1]) === key)
      return decodeURIComponent(itm[2]);
    else if (key === false)
      res[decodeURIComponent(itm[1])] = decodeURIComponent(itm[2]);
  }

  return key === false ? res : null;
}

Then you use it like this:

// get a single key (if it exists)
var t = getQueryString('site');
console.log(t);

// get the whole thing as an object
t = getQueryString();
console.log(t);
Chris Baker
  • 46,402
  • 12
  • 93
  • 112
  • 1
    Can you, please, tell me how to retrieve this object as a string? When I try to write by using document.write(t) it just writes the "object Object"... – sonjafon Jun 25 '11 at 23:24
  • You can't write an object as a string, but you can write a property of an object as a string. Using the second example above (get whole thing as an object), if you wanted to write the `site` key to the document, it would be `document.write(t.site)`. – Chris Baker Jun 27 '11 at 15:14