Thursday, July 11, 2013

change query string parameters or change parameters in url by jquery

//to add or update parameters in query string by passing an array like
    //[{ name: 'q', value: search }, { name: 'sort', value: sort}]
    function addQueryParameter(options) {

        /*
        * queryParameters -> handles the query string parameters
        * queryString -> the query string without the fist '?' character
        * re -> the regular expression
        * m -> holds the string matching the regular expression
        */
        var queryParameters = {}, queryString = location.search.substring(1),
    re = /([^&=]+)=([^&]*)/g, m;

        // Creates a map with the query string parameters
        while (m = re.exec(queryString)) {
            queryParameters[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
        }

        $.each(options, function (index, option) {
            queryParameters[option.name] = option.value;
        });

        //    // Add new parameters or update existing ones
        //    queryParameters['newParameter'] = 'new parameter';
        //    queryParameters['existingParameter'] = 'new value';

        /*
        * Replace the query portion of the URL.
        * jQuery.param() -> create a serialized representation of an array or
        *     object, suitable for use in a URL query string or Ajax request.
        */
        return $.param(queryParameters);

    }

No comments:

Post a Comment