Redirect user to external website on new tabe using onChange script but not working

trade_wala1
Tera Contributor

I am currently building a Catalog item and I want user to be redirected to an external website (e.g. Microsoft.com) if they select no in the group_hr variable question which is select box type. The goal is to not let them submit the form if they are selecting no in that variable. Below is my script but it is not doing anything when i tested. Please assist:

 

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

// Check if the new value is 'no'
if (newValue == 'no') {
// Message with a clickable link
var message = 'Please click <a href="https://www.microsoft.com" target="_blank">here</a> to get more information about this access.';

// Show message
g_form.showFieldMsg('group_hr', message, 'info');

// Redirect
top.window.open = "https://www.microsoft.com/";
}
}

 

I have tried window.location.href for redirect but no luck. It's also not displaying any messages that's in the code.

19 REPLIES 19

mate, got it sorted by using top.window.onbeforeunload = null

 

Thank you for your assistance.

@trade_wala1 

script I shared will work fine provided you are comparing correct value and Pop up is allowed in browser

it opened the url in new tab and also showed the message

When clicked on link it opened the url in new tab as well

So my script is working as expected

window open1.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur,

 

Yes I have made sure the pop and re-directs are allowed. I tried different browsers as well.

 

The end goal is to not let user click on submit button if they are not from group_hr. so as soon as they select 'no', it should redirect them to microsoft website for example so that they don't have a chance to click on submit.

@trade_wala1 

then you can let the url open in new tab and have onSubmit script to stop the form submission

OR

open the url in same tab so that they can't submit the catalog form

I believe I have provided enough guidance and you can take it from here based on your developer skills.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Thanks Ankur, 

 

I'll take it from here. Appreciate your assistance.