tags:

views:

1345

answers:

6

Just Consider : I am having the URL and with parameter

www.test.com/t.html&a=1&b=3&c=m2-m3-m4-m5

I just want to get the value of 'c' . I need to get the all value of c

because i just try to read the url but i got only one m2 . But i want all value using Java Script

+1  A: 

There are lots of implementations of parsing a query string using javascript, here are a couple

http://adamv.com/dev/javascript/querystring

http://prettycode.org/2009/04/21/javascript-query-string/

objects
Java != JavaScript
Ates Goral
woops, that’s was typo :)
objects
+5  A: 

JavaScript has nothing built in for handling query string parameters.

You could access location.search, which would give you from the ? character on to the end of the fragment identifer (#foo), whichever came first.

This suggests that you have written (or found some third party) code for reading the query string and accessing just the bit that you want - but you haven't shared it with us, so it is hard to say what is wrong with it.

The code I generally use is this:

var QueryString = function () {
  // This function is anonymous, is executed immediately and 
  // the return value is assigned to QueryString!
  var query_string = {};
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
     // If first entry with this name
    if (typeof query_string[pair[0]] === "undefined") {
      query_string[pair[0]] = pair[1];
     // If second entry with this name
    } else if (typeof query_string[pair[0]] === "string") {
      var arr = [ query_string[pair[0]], pair[1] ];
      query_string[pair[0]] = arr;
     // If third or later entry with this name
    } else {
      query_string[pair[0]].push(pair[1]);
    }
  } 
    return query_string;
} ();

You can then access QueryString.c

David Dorward
I believe you mean location.search, but I see in your source that it is correct there.
Blixt
Whoops. I write the prose from memory but copied real code for the example. Thanks for spotting that.
David Dorward
You should URL-decode the names and values.
Ates Goral
A: 

You can get the current location in location.href, then you can split everything after the question mark:

var params = {};

if (location.search) {
    var parts = location.search.substring(1).split('&');

    for (var i = 0; i < parts.length; i++) {
        var nv = parts[i].split('=');
        if (!nv[0]) continue;
        params[nv[0]] = nv[1] || true;
    }
}

// Now you can get the parameters you want like so:
var abc = params.abc;
Blixt
location.search is more appropriate
annakata
I suppose you're right. I changed my example.
Blixt
What about URL-decoding the parameter names and values?
Ates Goral
+1  A: 

i take it from a link

http://www.netlobo.com/url_query_string_javascript.html

function gup( name ){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
var regexS = "[\\?&]"+name+"=([^&#]*)";  
var regex = new RegExp( regexS );  
var results = regex.exec( window.location.href ); 
 if( results == null )    return "";  
else    return results[1];}

The way that the function is used is fairly simple. Let's say you have the following URL:

http://www.foo.com/index.html?bob=123&amp;frank=321&amp;tom=213#top

You want to get the value from the frank parameter so you call the javascript function as follows:

var frank_param = gup( 'frank' );
Haim Evgi
A: 

Here's a tool I build, prob you want this:

http://jrharshath.qupis.com/urlparser/

You can get its source code from View->Source Code (I'm using IE now :( )

Here Be Wolves
+2  A: 

Most implementations I've seen miss out URL-decoding the names and the values.

Here's a general utility function that also does proper URL-decoding:

function getQueryParams(qs) {
    qs = qs.split("+").join(" ");

    var params = {}, tokens;

    while (tokens = /[?&]?([^=]+)=([^&]*)/g.exec(qs)) {
        params[decodeURIComponent(tokens[1])]
            = decodeURIComponent(tokens[2]);
    }

    return params;
}

//var query = getQueryParams(document.location.search);
//alert(query.foo);
Ates Goral