I can't use window.location.href on Server but can on Client

Shane18
Kilo Expert

On my Service Portal widget, I can only use window.location.href on the client and not the server, it says "Server JavaScript error "window" is not defined.", how can I retrieve the current URL on the server, or alternatively pass it from the client to the server? Thanks!

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hello Shane,

That's because window.location.href is client side and won't work in server side. Even if you use it on client side it won't work in service portal.

If you want to fetch url in server side then have this in server side script

gs.action.getGlideURI()

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Thanks Ankur, it doesn't return the URL though, it returns this: api/now/sp/page?api=api&portal_id=92edae50db021b0052f59c27db961913&request_uri=%2ftesm_hive&time=1534252013361

How do I get the URL?

Omkar Mone
Mega Sage

Hi Shane,

 

In Client script do something like this :-

var c = this;

Define a c.data.url = window.location.href;

c.server.update();

 

At server side get the url by :-

Do something like this input.url.

 

Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.

 

Regards,

Omkar Mone

 www.dxsherpa.com

find_real_file.png

 

ChrisBurks
Mega Sage

Not sure if this will help but if the goal is to retrieve the instance the script runs on dynamically then you could also build the url like the following on the server side:

 

var snInstance = gs.getProperty('instance_name');

var url = snInstance + '.service-now.com'; 

You could also prepend that with the https:// if needed

If the portal suffix is needed then append url with something like this:

url += "/" + $sp.getPortalRecord().getValue('url_suffix')

 

All together

var snInstance = gs.getProperty('instance_name');

var url = 'https://' + snInstance + '.service-now.com/';

url += $sp.getPortalRecord().getValue('url_suffix');

url; // output if on oob portal would be similar to this https://myinstancename.service-now.com/sp