I am using window.onbeforeunload to call socket before my page unloads, onbeforeunload shows a pop-up to confirm 'if we want to leave the page'. I want my JavaScript code to auto check the condition and don't show me the alert on the browser and leave the page automatically.
My code-
<div ng-app="myApp" ng-controller="myCtrl">
<button ng-click="redirect()">Redirect to google</button>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.redirect=()=>{
window.location.href="https://google.com";
}
});
</script>
<script>
window.onbeforeunload = (e) => {
console.log("before unloading the page");
return "";
};
</script>
How can I stop showing the alert whenever I click to redirect button!!