Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How can I get query string values in JavaScript?

user-image
Question added by Zaid Rabab'a , Technical Team Lead , ESKADENIA Software
Date Posted: 2013/11/20
Ahmed Abouzaid
by Ahmed Abouzaid , Senior Software Engineer / .Net / Outsystems , Emaratech

You can get query string value by this function.

function getQueryStr(n) {

    var query = location.search.split('&'+n+'=')[1];

    if (!queryquery= location.search.split('?'+n+'=')[1];

    return query? decodeURIComponent(query.split('&')[0]):null;

}

Clodelio Delfino
by Clodelio Delfino , Managing Consultant , Startup Company

var urlPath = window.location.pathname.split( '/' );

 

then just do some string parsing on urlPath variable...

Taha Hanif
by Taha Hanif , Software Developer , Design Your Site

if(!window.location.getParameter ){ window.location.getParameter =function(key){function parseParams(){varparams={}, e, a =/\\+/g,// Regex for replacing addition symbol with a space r =/([^&=]+)=?([^&]*)/g, d =function(s){return decodeURIComponent(s.replace(a," "));}, q = window.location.search.substring(1);while(e = r.exec(q))params[d(e[1])]= d(e[2]);returnparams;}if(!this.queryStringParams)this.queryStringParams = parseParams();returnthis.queryStringParams[key];};}

Bowsil Ameen
by Bowsil Ameen , Sharepoint Development officer / architect , Etihad Airways

Hi Zaid,

Here is the small javascript function which can take variable as parameter

function getQuerystring(key)

 {  

key = key.replace(/[\\[]/,"\\\\\\[").replace(/[\\]]/,\\\\\\]); 

var regex = new RegExp("[\\\\?&]"+key+"=([^&#]*)"); 

var qs = regex.exec(window.location.href);

  if(qs == null)

alert("no Q string found..."); 

else alert(qs[1]);

 }

Atif Aqeel
by Atif Aqeel , Founder And Director , Xesense

function getParameterByName(name){ name = name.replace(/[\\[]/,"\\\\\\[").replace(/[\\]]/,"\\\\\\]");var regex =newRegExp("[\\\\?&]"+ name +"=([^&#]*)"), results = regex.exec(location.search);return results ==null?"": decodeURIComponent(results[1].replace(/\\+/g," "));}

WE can get string values by using alert box("message")

islam khalil
by islam khalil , Technical Manager , iCloudit

 

var query = window.location.search;

 

JQUERY:

http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html

 

Google :

function(b){var c =typeof b ==="undefined";if(a !== h && c)return a;for(var d ={}, b = b || k[B][vb], e = b[p]("?"), f = b[p]("#"), b =(f ===-1? b[Ya](e +1):[b[Ya](e +1, f - e -1),"&", b[Ya](f +1)][K](""))[z]("&"), e = i.dd ? ia : unescape, f =0, g = b[w]; f < g;++f){var l = b[f][p]("=");if(l !==-1){var q = b[f][I](0, l), l = b[f][I](l +1), l = l[Ca](/\\+/g," ");try{ d[q]= e(l)}catch(A){}}} c &&(a = d);return d }

 

 

JS : 

 

function getParameterByName(name){ name = name.replace(/[\\[]/,"\\\\\\[").replace(/[\\]]/,"\\\\\\]");var regex =newRegExp("[\\\\?&]"+ name +"=([^&#]*)"), results = regex.exec(location.search);return results ==null?"": decodeURIComponent(results[1].replace(/\\+/g," "));}

 

Sandhya Binoy
by Sandhya Binoy , Software Professional , Karrox technologies

 

document.write(location.search);

George Dimitrov
by George Dimitrov , Unix System Administrator , ADVANCED.IO

Hello Zaid,

Just split up the parameters into an associative-array style:

var urlParams;

(window.onpopstate = function () {

    var match,

        pl     = /\\+/g,  // Regex for replacing addition symbol with a space

        search = /([^&=]+)=?([^&]*)/g,

        decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },

        query  = window.location.search.substring(1);

 

    urlParams = {};

    while (match = search.exec(query))

       urlParams[decode(match[1])] = decode(match[2]);

})();

 

Example querystring:

?i=somemore&mode=xxxx&sid=123456&enc=+Voala%20&empty

 

Result:

 

urlParams = {

    enc: "Voala",

    i: "somemore",

    mode: "xxxx",

    sid: "123456",

    empty: ""

}

 

alert(urlParams["mode"]);

// -> "front"

 

alert("empty" in urlParams);

 

// -> true

 

Server-side preprocessing with PHP and native JSON functions.

 

<script>var urlParams = <?php echo json_encode($_GET, JSON_HEX_TAG);?>;</script>

Muhammad Majid Saleem
by Muhammad Majid Saleem , Senior PHP Developer / Project Manager , SwaamTech

I found a best example for this. Please have a look on following URL:

http://www.darlesson.com/jquery/querystring/?language=en&province=Alberta&province=Ontario&province=Quebec

More Questions Like This