Reputation: 7004
Is there a way in AngularJS or Javascript to detect that the user is leaving my website (can be closing his browser or entering another page) and to prompt a modal? In this modal I want to ask the user why he is leaving.
Also, is it OK to have something like this? Will my website be blacklisted due to this "annoying: feature?
Upvotes: 0
Views: 204
Reputation: 48968
To combat unwanted pop-ups, modern browsers severely limit what JavaScript can do before a page unloads.
From the Docs:
Window: beforeunload event
The
beforeunload
event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point.This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page. If the user confirms, the browser navigates to the new page, otherwise it cancels the navigation.
For more information, see
Upvotes: 1
Reputation: 386
With Angular use
$scope.$on("$locationChangeStart",function(){
//do your stuff here
});
to detect when the user leaves the page.
If you use alert('your message');
, the browser will problably give the user an option to block alerts from your site.
Upvotes: 0