ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

How can I get query string values in JavaScript?

user-image
تم إضافة السؤال من قبل Zaid Rabab'a , Technical Team Lead , ESKADENIA Software
تاريخ النشر: 2013/11/20
Ahmed Abouzaid
من قبل 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
من قبل Clodelio Delfino , Managing Consultant , Startup Company

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

 

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

Taha Hanif
من قبل 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
من قبل 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
من قبل 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," "));}

islam khalil
من قبل 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
من قبل Sandhya Binoy , Software Professional , Karrox technologies

 

document.write(location.search);

George Dimitrov
من قبل 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>

المزيد من الأسئلة المماثلة