Can we redirect using client script

HarshTimes
Tera Guru

Can we redirect to any other page or homepage using client script.
I need that if the IF condition true then redirect to homepage.Please help me out

1 ACCEPTED SOLUTION

Ripu Daman1
Tera Expert

I know this post is old and Harsh might have got his answer but just in case someone stumbles upon this post and looking for a way to redirect using client script then below code snippet might help:

 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Type appropriate comment here, and begin script below
if (newValue == 'expense_freight_realestate'){
var redirectURL = 'https://www.google.com/'; //url to redirect; this approach works in platform and Service Portal view
top.window.location = redirectURL ;
}
}

 

Please note that in case you want to open up external link within the main ServiceNow Frame then you may use g_navigation.open() but this approach doesnt work when the Service Catalog/form will be used in Service Portal.

View solution in original post

17 REPLIES 17

Indeed, our dev teams have protected the necessary JavaScript classes and they are not accessible anymore.


You will need to work with location.assign as mentioned by Pratul.



I might be an idea to raise an Enhancement request via HI. Our PM's are listening and could surely put this on the backlog if enough interest exists.


Hi Mark.



Using


location.assign("https://www.google.com");




It is redirecting me to a blank page rather than the actual destination.




Regards


Pratul Agarwal


pyushm
Kilo Contributor

You can write a script in UI page directly to redirect.


like



<script>


        window.location.href = "URL";


</sccript>


Ripu Daman1
Tera Expert

I know this post is old and Harsh might have got his answer but just in case someone stumbles upon this post and looking for a way to redirect using client script then below code snippet might help:

 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Type appropriate comment here, and begin script below
if (newValue == 'expense_freight_realestate'){
var redirectURL = 'https://www.google.com/'; //url to redirect; this approach works in platform and Service Portal view
top.window.location = redirectURL ;
}
}

 

Please note that in case you want to open up external link within the main ServiceNow Frame then you may use g_navigation.open() but this approach doesnt work when the Service Catalog/form will be used in Service Portal.

You saved my day with this! Thanks!!