https://www.servicenowguru.com/scripting/client-scripts-scripting/parse-url-parameters-client-script/

Parse URL Parameters in a Client Script

	Here’s a useful script I’ve used before to grab parameters from a URL in Service-now.com. Simply pass the name of the parameter into the function and it will return the corresponding parameter value.

	So if you had a Service-now.com URL that looked like this…

https://demo.service-now.com/incident.do?sys_id=-1&sysparm_query=active=true&sys_myparm=abcde You could get the value of the ‘sys_myparm’ URL parameter by calling the function below like this… `var myparm = getParmVal('sys_myparm');

Here’s the ‘getParmVal’ function…

function getParmVal(name){

var url = document.URL.parseQuery();

if(url[name]){

    return decodeURI(url[name]);

}

else{

    return;

}

}`

I’ve often used this to automatically populate catalog variables based on a redirect URL. For example, if I were redirecting to a catalog item and I wanted to pass in a couple of variable values I could construct my URL like this… var url = 'com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=e826b8c50a0a3c1e019410bb1031d102' + '&sysparm_category=hardware' + '&sysparm_comments=Hello World!!!'; This URL contains 3 major pieces of information… Pointer to the catalog item – identified by item sys_id (‘com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=e826b8c50a0a3c1e019410bb1031d102’) A parameter containing a category value (‘&sysparm_category=hardware’) A parameter containing a comments value (‘&sysparm_comments=Hello World!!!’) By passing parameters in through the catalog item URL you can use an ‘onLoad’ catalog client script (set against the item or a variable set) to pull those parameters out and populate them into your catalog item variables like this…

function onLoad() {

   //Populate the variables with the parameters passed in the URL

   //Use the 'getParmVal' function below to get the parameter values from the URL

   var cat = getParmVal('sysparm_category');

   var comments = getParmVal('sysparm_comments');

   if(cat){

      g_form.setValue('my_category_variable',cat);

   }

   if(comments){

      g_form.setValue('my_comments_variable',comments);

   }

}

function getParmVal(name){

    var url = document.URL.parseQuery();

    if(url[name]){

        return decodeURI(url[name]);

    }

    else{

        return;

    }

}
	When working in the Service Portal, ServiceNow severely restricts the objects available for use in client scripts, causing scripts like this to break. Brad Tilton provided this workaround on the ServiceNow community for the ‘getParameterValue’ function that is designed to work around this issue.
	`function getParameterValue(name) {  

name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");  

var regexS = "[\\?&]" + name + "=([^&#]*)";  

var regex = new RegExp(regexS);  

var results = regex.exec(top.location);  

if (results == null) {  

    return "";  

} else {  

    return unescape(results[1]);  

}  

}` Please note that this code snippet should be used within a client script or catalog client script on a form you’re rendering through the Service Portal. If you’re working within a widget and want to use a url parameter you would want to use angular’s $location service.