$window.onbeforeunload on Service Portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2018 07:14 PM
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 ?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2018 08:32 PM
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'
});
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2020 01:27 AM
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