Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to convert query string to json object in javascript?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.17k
    Comment on it

    You need to parse an URL and return all the parameters from the URL, you can use the below javascript function:-

    var getURLParameters = function(url) {
        var result = {};
        var searchIndex = url.indexOf("?");
        
        if (searchIndex == -1 ) return result;
            var sPageURL = url.substring(searchIndex +1);
            var sURLVariables = sPageURL.split('&');
            for (var i = 0; i < sURLVariables.length; i++) {       
                var sParameterName = sURLVariables[i].split('=');      
                result[sParameterName[0]] = sParameterName[1];
            }
            return result;
    };

    Usage:

    var url = 'http://www.example.com/?id=1&user=username&password=password;
    var params = getURLParameters(url);
    console.log(params);

    Result:

    Object {id: 1, user: "username", password: "password"}

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: