jQuery add param to url function

Sam Deering
Share

This jQuery utility function checks if a paramter is present in the current page URL and if it doesn’t exist it adds it and returns the url in full. Could be useful if you needed to do an ajax request to update a database with new form data and simply want to redirect to the same url but with an updated flag to show an updated box.

updated-ajax-box

(function($,W,D)
{
    var JQUERY4U = {};

    JQUERY4U.UTIL =
    {
        /**
          * Add a parameter to url if doesn't already exist
          * @param param - the parameter to add
          * @param value - the value of the parameter
          * @return url - the url with the appended parameter
          */
        addParamToUrl: function(param, value)
        {
            //check if param exists
            var result = new RegExp(param + "=([^&]*)", "i").exec(W.location.search);
            result = result && result[1] || "";

            //added seperately to append ? before params
            var loc = W.location;
            var url = loc.protocol + '//' + loc.host + loc.pathname + loc.search;

            //param doesn't exist in url, add it
            if (result == '')
            {
                //doesn't have any params
                if (loc.search == '')
                {
                    url += "?" + param + '=' + value;
                }
                else
                {
                    url += "&" + param + '=' + value;
                }
            }

            //return the finished url
            return url;
        }
    }

    //example usage
    var updatedUrl = JQUERY4U.UTIL.addParamToUrl('updated', 'true');
    console.log(updatedUrl);
    //input: http://jquery4u.com/index.php
    //output: http://jquery4u.com/index.php?updated=true

})(jQuery, window, document);