I can't use window.location.href on Server but can on Client
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-14-2018 04:22 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-14-2018 04:46 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-14-2018 06:07 AM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-14-2018 06:21 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-14-2018 07:06 AM
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