1

I'm looking for an dependency-free implementation of jQuery.param(). I'm trying to create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request, without using jQuery.

I searched for some time now, but everyone just seems to use $.param()...

Thank you in advance!

8
  • 2
    stackoverflow.com/questions/901115/… Commented Jul 5, 2013 at 16:29
  • perhaps the easiest thing to do would be to look at the code for $.param()/.serialize()... Then you can replace all the jquery specific stuff with your own implementations. Commented Jul 5, 2013 at 16:32
  • @FelipeGavilan That's the opposite of jQuery.param. Commented Jul 5, 2013 at 16:32
  • Is there some reason you can't just use JSON.stringify()? Commented Jul 5, 2013 at 16:33
  • @CJX: that will just create a JSON string, he wants a URL encoded string. Commented Jul 5, 2013 at 16:35

4 Answers 4

1

I found the best and optimal way on How to get query string values using JavaScript. Checkout the below example to fetch the query string.

var queryString = window.location.search || '';
var keyValPairs = [];
var params      = {};
queryString     = queryString.substr(1);

if (queryString.length)
{
   keyValPairs = queryString.split('&');
   for (pairNum in keyValPairs)
   {
      var key = keyValPairs[pairNum].split('=')[0];
      if (!key.length) continue;
      if (typeof params[key] === 'undefined')
         params[key] = [];
      params[key].push(keyValPairs[pairNum].split('=')[1]);
   }
}

Usage of the above script

//url=http://stackoverflow.com/how-to-get-query-string-values-in-javascript?query=123&list=default
params['query'];
//Output ["123"]

params['list'];
//Output ["default"]

//Note: If the query string value is empty the method will return the value as empty string.
Sign up to request clarification or add additional context in comments.

Comments

0

if You need to get query string value using javascript

function getUrlVars()
    {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }

Comments

0

If your object is a one dimension array like this:

 var params = { input1:x, input2:y, input3: z };

Then a simple for loop can do the trick to serialize the object

var vals = '';
for(var key in params){
    vals += key + '=' + paramas[key];
}

Comments

0

Create a blank .js file (eg. GetQueryString.js) and copy the following code in it

function getParameterByName(name) {

var match = RegExp('[?&]' + name + '=([^&]*)')
                .exec(window.location.search);

return match && decodeURIComponent(match[1].replace(/\+/g, ' '));

}


var urlParams = {};
(function () {
var e,
    a = /\+/g,
    r = /([^&=]+)=?([^&]*)/g,
    d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
    q = window.location.search.substring(1);

while (e = r.exec(q))
    urlParams[d(e[1])] = d(e[2]);
})();

Include GetQueryString.js file in your code

    <script type="text/javascript" src="~/GetQueryString.js" /> 

If your key is in URL as follows

http://www.domain.com?key=123

key can be fetched from above URL by following call

var queryStringValue = getParameterByName("key"); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.