- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-04-2023 11:13 AM
I need to parse the URL for a catalog item. I am using the following onload script but get browser error.
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;
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-04-2023 10:45 PM - edited ā03-04-2023 10:46 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-04-2023 12:18 PM
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)
};
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-04-2023 05:25 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-04-2023 10:45 PM - edited ā03-04-2023 10:46 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-05-2023 06:27 AM
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.