How to get a PARAMETER from a URL String [jQuery]

Summary:

Sometimes in jQuery you need to grab a paramater from a URL.
e.g. http://dummy.com/?technology=jquery&blog=jquerybyexample

Solution:

  1. Add the following code to your javascript file:

    var getUrlParameter = function getUrlParameter(sParam) {
        var sPageURL = decodeURIComponent(window.location.search.substring(1)),
            sURLVariables = sPageURL.split('&'),
            sParameterName,
            i;
    
        for (i = 0; i < sURLVariables.length; i++) {
            sParameterName = sURLVariables[i].split('=');
    
            if (sParameterName[0] === sParam) {
                return sParameterName[1] === undefined ? true : sParameterName[1];
            }
        }
    };
  2. Now you can target a paramater as follows:

    var tech = getUrlParameter('technology');
    var blog = getUrlParameter('blog');