window.open written in a client script and its not working on Service Portal

prabh
Mega Expert

Hi Folks,

I have a catalog item and in that there is onchange client script which opens different URLs based on the value selected using window.open() function.

But this script is not working on Service Portal. I have changed the UI type of the script to "Both" as well.

Please help.

Thanks

Preety Arora

23 REPLIES 23

nathanfirth
Tera Guru

In Angular you have to use the "$window" service instead of just "window":



function($window) {
  $window
.open('http://google.com');
};


prabh
Mega Expert

if window is not supported in SP then what can be the alternative approach for below script (which is currently being used)



if(g_form.getValue('issue_name') == "Restore issue")


  {


alert(window);   // this is returning null


top.window.location.href = "xxxxxxx";       // this is working but opening in same page.


window.open("xxxxx");     // not working


}



I tried $window as well . But not working.


The code below worked for me in the Service Portal in Helsinki patch6-hotfix1.


function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }




//Redirect if the answer is no


  var variableName = g_form.getValue('variable_name');


  if (variableName == 'No'){


  var win = top.window.open('http://www.google.com', '_blank');



  if(win){


  //Browser has allowed it to be opened


  alert('You are being redirected to another site.');


  win.focus();


  }else{


  //Broswer has blocked it


  alert('Please allow popups for this site.');


  }


  }


}


Hersh D
Giga Expert

Note: the 'top' and 'self' objects can be used in client scripts in scoped apps.


- top refers to the top-most/parent window


- self refers to the window/iframe (gsft_main) that contains the detailed view - e.g. the incident form.



Example:


top.location.href - will return https://instance.service-now.com/nav_to.do?uri=/incident.do%3Fsys_id%3Dd45e4534dccb7400cac937e91440d...


self.location.href - will return https://instance.service-now.com/incident.do?sys_id=d45e4534dccb7400cac937e91440d158