How to extract or retrieve parameter values from url using jQuery?

Let us consider the following sample Url, from which we want to extract the parameter values.
window.location.href = "./EquipmentCostList.aspx?headerId=" + 123+"&WorkOrderNumber="+123+"&WeekEndindDate="+2019/08/12;
If we see the above sample url we can find the parameters like headerId,WorkOrderNumber,etc..

So, to extract these parameter values from the url, we can easily write some jQuery code as shown below,

jQuery Code:

$(document).ready(function () {
//below method is used to retrieve the single parameter value based the parameter name sent.
            var getUrlParameter = function getUrlParameter(sParam) {
                var sPageURL = 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 :                                                                                decodeURIComponent(sParameterName[1]);
                         }//if block
                    }//for loop
                };//function close

            //calling the method to get value 
            var hId = getUrlParameter('headerId');
            var WNumber = getUrlParameter('WorkOrderNumber');
            var WDate = getUrlParameter('WeekEndindDate');

            alert(hId);
            alert(WNumber);
            alert(WDate);
});

Comments

Popular posts from this blog

Top MNC Interview Questions- Full Stack Developer

Interview Questions-2

How to get the user details, user current level, next level, Rank in sql server