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

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

@Ankur Bawiskar 

This gave the complete URL instead of the instance URL. But how can I make it a clickable link under the field using 

g_form.showFieldMsg('field_name', url_var, 'info');
 
Or as Alert() pop up?

@KeDix 

you can't show link in field message as it's not supported in showFieldMsg()

if you want instance URL then no direct way available

1) either use Display business rule + g_scratchpad in onLoad

OR

2) onLoad with GlideAjax

💡 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

@KeDix 

Thank you for marking my response as helpful.

💡 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