Script

Wednesday, November 20, 2019

Javascript : Accessing Query String Parameters

The below script is to get the query string values passed to CRM web resource,
       
function parseQueryString(qs, opts) {
    var queryString = qs || window.location.search || "";
    var queryStringRegularExpression = /(?:\?|&|^)([^=]+)=([^&]*)/ig;
    var qsMatch = queryStringRegularExpression.exec(queryString);
    var paramKey;
    var paramVal;
    while (!!qsMatch) {
        paramKey = decodeURIComponent(qsMatch[1] || "").toLowerCase();
        paramVal = decodeURIComponent(qsMatch[2] || "");
        if ((paramKey === "data") && !!paramVal) {
            parseQueryString(paramVal, opts);
        } else if (!!paramKey && !!paramVal) {
            opts[paramKey] = paramVal;
        }
        qsMatch = queryStringRegularExpression.exec(queryString);
    }
}
       
 
Usage of Query String method :
       
var qsOptions = {};
parseQueryString(location.search, qsOptions);
 
// Get your parameters here
var name = qsOptions.name;
var country = qsOptions.country;
       
 
Sample Webresource URL
       
http://servername/WebResources/new_/AccesingQueryString.htm?Data=name%3DfullName%26country%3DIndia

 

No comments:

Post a Comment