I want to capture the page reload event using JavaScript and then redirect to a different URL (i.e. load the different page in the same browser tab) instead of the letting the existing page reload. My code is here -
<script type="text/javascript">
window.onbeforeunload = function(e) {
window.location = "https://www.google.com";
e.preventDefault();
};
</script>
The code gets executed when I right click in Chrome and then click reload but www.google.com does not load in the tab - instead the page reloads in the same tab.
What should be changed here?