top.window.location not working in service portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2021 01:21 AM
Hi ,
I have a requirement to redirect the users on the same tab if a particular ci is selected in a form . I am using top.window.location but its quite finicky .its not working and throwing me an error of "browser console error " upon inspecting it says top.window.location is an invalid function.
Any alternatives or suggestions to this.
My On change client script is as below:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ci = g_form.getValue('cmdb_ci');
if (ci== 'd4e7b6766fd8460026a6bce0be3hd657') {
var url = "/sp?id=sc_cat_item&sys_id=ae5e9fcd1bf764d0607beb11604bcba1";
top.window.location(url);
}
}
Any leads on this is highly appreciated!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2021 01:26 AM
I would recommend you to use this below code in widget controller
function ($window) {
var c = this;
c.quantity = 1;
var isViewedOnDesigner = $window.location.href.toString().indexOf('$spd.do') > -1;
if(isViewedOnDesigner){
c.timeout = $timeout(function(){
//you can put some modal / pop up here
console.log('Session time out');
}).then(function() {
$window.location.reload(true);
}, function() {
$window.location.reload(true);
})
}, c.data.timeout * 60000, false);
Reference Link
https://community.servicenow.com/community?id=community_question&sys_id=10791a56db75930058dcf4621f961951

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2021 01:31 AM
Hi Naresh,
I believe you are using this in a catalog client script, change the isolate script to false and if it still doesn't work could you please give a try using "window.location.href(url)" and changing the isolate script to false.
Kindly mark my answer as Correct and Helpful based on the Impact.
Regards,
Alok
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2021 01:51 AM
can you update as this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ci = g_form.getValue('cmdb_ci');
if (ci== 'd4e7b6766fd8460026a6bce0be3hd657') {
var url = "/sp?id=sc_cat_item&sys_id=ae5e9fcd1bf764d0607beb11604bcba1";
top.location.href = url;
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader