$window.onbeforeunload on Service Portal

CA5
Tera Contributor

Body HTML =============>

<input type="button" id="fname" ng-click="myFunction()">

 

Client Controller =============>

var nAgt;
var verOffset;

$scope.myFunction = function (){

alert('First Alert');

$window.onbeforeunload = function(event) {
alert('Second Alert');
nAgt = navigator.userAgent;
//if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
event.returnValue = "Please Update";
console.log('onbeforeunload ==> ' + navigator.userAgent);
//}
};
}

It displays first alert but does not show second alert.

How to use $window.onbeforeunload inside a function on Client Controller ?

 

2 REPLIES 2

Omkar Mone
Mega Sage

Hi 

You can go with something like this by making an async call to false :-

$(window).unload(function () {
   $.ajax({
     type: 'GET',
     async: false,
     url: 'SomeUrl.com?id=123'
   });
});

Hello CA,

 

If my response helped you, could you mark this thread as closed by marking my answer as correct answer?

 

Regards

Omkar Mone

ServiceNow Community 2020 MVP