- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
How to get the instance URL using the OnLoad Client Script of the Form?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
this will work
var url = top.location.href;
alert(url);
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday - last edited yesterday
Hi @KeDix ,
it depends on browsers sometimes!,
Are you trying to get the instance URL or the current opened URL?
If its for the Instance URL - why don't you try script include and use the system property as below
Client script:
function onLoad() {
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('GetInstanceUrl');
ga.addParam('sysparm_name', 'getUrl');
ga.getXMLAnswer(response);
function response(url){
alert(url);
}
}
Script include:
var GetInstanceUrl = Class.create();
GetInstanceUrl.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getUrl: function(){
return gs.getProperty('glide.servlet.uri');
},
type: 'GetInstanceUrl'
});
Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Here is the solution:
Get the Instance URL without using Script Include:
var instanceURL = top.location.href; get full URL
instanceURL = instanceURL.slice(0, instanceURL.indexOf("/now")); // Instance URLg_form.addErrorMessage(htmlMessage);
g_form.addDecoration('field_name','icon-alert',"No relation",'color-red');
g_form.addDecoration('field_name','icon-locked',"No relation",'color-red');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday - last edited yesterday
Hi @KeDix ,
it depends on browsers sometimes!,
Are you trying to get the instance URL or the current opened URL?
If its for the Instance URL - why don't you try script include and use the system property as below
Client script:
function onLoad() {
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('GetInstanceUrl');
ga.addParam('sysparm_name', 'getUrl');
ga.getXMLAnswer(response);
function response(url){
alert(url);
}
}
Script include:
var GetInstanceUrl = Class.create();
GetInstanceUrl.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getUrl: function(){
return gs.getProperty('glide.servlet.uri');
},
type: 'GetInstanceUrl'
});
Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
I know this solution, and it works too. But trying to see if it can work without a Script Include..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Here is the solution:
Get the Instance URL without using Script Include:
var instanceURL = top.location.href; get full URL
instanceURL = instanceURL.slice(0, instanceURL.indexOf("/now")); // Instance URLg_form.addErrorMessage(htmlMessage);
g_form.addDecoration('field_name','icon-alert',"No relation",'color-red');
g_form.addDecoration('field_name','icon-locked',"No relation",'color-red');
