Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Script to parse URL to get parameters šŸ¤·ā€ā™‚ļø

Luis Roman1
Tera Contributor

I need to parse the URL for a catalog item. I am using the following onload script but get browser error.

LuisRoman1_0-1677957165737.png

Can someone please help me overcome this problem. My experience with JavaScript is limited so collaboration will be helpful.

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('sys_parm');
 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;
}
}

1 ACCEPTED SOLUTION

Based on what I'm guessing is wanted, it would be:

 

function onLoad () {
	var getParmVal = getSearchParams();
	var cat = getParmVal('sys_parm');
	var comments = getParmVal('???');

	if (cat) {
		g_form.setValue('my_category_variable', cat);
	}

	if (comments) {
		g_form.setValue('my_comments_variable', comments);
	}
}

function getSearchParams () {
	var url = new URL(self.location.href);
	var searchParams = url.searchParams;

	return function get (name) {
		return searchParams.get(name)
	};
}

 

But the parameter name (sys_parm) doesn't seem right and I don't know the parameter name for comments.

View solution in original post

9 REPLIES 9

-O-
Kilo Patron

The code below should work:

var url = new URL(self.location.href);
console.log(url.searchParams.get('sys_parm'));

document.URL is a string and as such it has no parseQuery - at least not by standards.

Though the code fails already at document since it is not allowed in Portal - the original global variable is "shadowed" with a context one that has value null.

Putting it all together:

function onLoad() {
  var currentSearchParams = getSearchParams();

  // This would print "sc_cat_item" on the console
  console.log(currentSearchParams('id'));

  // ...
  
  function getSearchParams() {
    const url = new URL(self.location.href);
    const searchParams = url.searchParams;

    return function(name) {
      return searchParams.get(name)
    };
  }
}

Luis Roman1
Tera Contributor

Thank you for the replied. However, because my javascript is very limited I am not able to figure out how to fix the script that I included in the post with your recommendation. Could you give it another try please? I would like to understand the solution and/or the fix. 

Your collaboration will be appreciated.

 

Luis

Based on what I'm guessing is wanted, it would be:

 

function onLoad () {
	var getParmVal = getSearchParams();
	var cat = getParmVal('sys_parm');
	var comments = getParmVal('???');

	if (cat) {
		g_form.setValue('my_category_variable', cat);
	}

	if (comments) {
		g_form.setValue('my_comments_variable', comments);
	}
}

function getSearchParams () {
	var url = new URL(self.location.href);
	var searchParams = url.searchParams;

	return function get (name) {
		return searchParams.get(name)
	};
}

 

But the parameter name (sys_parm) doesn't seem right and I don't know the parameter name for comments.

Luis Roman1
Tera Contributor

Mega Sage thank you a lot. Wow you are amazing. Your solution works very very good. It execute in the desktop and portal. Thank you again.