How to How to get the instance URL using get the instance URLusing OnLoad Client Script of the Form?

KeDix
Tera Guru

How to get the instance URL using the OnLoad Client Script of the Form?

window.location.origin; does not work 
3 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@KeDix 

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! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

Hemanth M1
Giga Sage
Giga Sage

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

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

View solution in original post

KeDix
Tera Guru

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 URL
 
Clickable link on Form:
g_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');

 

View solution in original post

7 REPLIES 7

Hemanth M1
Giga Sage
Giga Sage

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

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

@Hemanth M1 

I know this solution, and it works too. But trying to see if it can work without a Script Include..

KeDix
Tera Guru

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 URL
 
Clickable link on Form:
g_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');