I'm trying to show a confirmation pop before user close the tab or went to another tab like facebook, gmail, GoDaddy & others do.
My code working for Firefox but not for other browser like chrome, safari etc.
<script type="text/javascript">
var hook = true;
window.onbeforeunload = function() {
if (hook) {
return "Did you save"
}
}
function unhook() {
hook=false;
}
</script>
Call unhook() onClick for button and links
<a href="" onClick="unhook()">No Block URL</a>
Please help me to get this fixed.
I'm trying to show a confirmation pop before user close the tab or went to another tab like facebook, gmail, GoDaddy & others do.
My code working for Firefox but not for other browser like chrome, safari etc.
<script type="text/javascript">
var hook = true;
window.onbeforeunload = function() {
if (hook) {
return "Did you save"
}
}
function unhook() {
hook=false;
}
</script>
Call unhook() onClick for button and links
<a href="http://example." onClick="unhook()">No Block URL</a>
Please help me to get this fixed.
Share Improve this question edited Feb 13, 2019 at 8:59 Ameena sana 813 bronze badges asked Jan 17, 2019 at 5:30 JK SandhiyaJK Sandhiya 1833 silver badges13 bronze badges 5- Do you know the modal? Do you want modal like this?getbootstrap./docs/4.1/ponents/modal – Arman Bagheri Commented Jan 17, 2019 at 5:37
- Possible duplicate of Confirmation before closing of tab/browser – adiga Commented Jan 17, 2019 at 5:53
- any error in console? – Prashanth Benny Commented Jan 17, 2019 at 6:40
- It's already asked please check below link stackoverflow./questions/19263277/… – Manish Commented Jan 17, 2019 at 7:08
- It's already asked please check stackoverflow./questions/19263277/… – Manish Commented Jan 17, 2019 at 7:10
2 Answers
Reset to default 4If you take a look at the api of window.beforeunload()
, you can see that, though widely the basic unload event is supported, a custom message can only be set in internet explorer and certain versions of some browsers. So just use the normal standard message.
This feature (custom messages) was often exploited by malicous sites to interact with user in a harmful or malipulative way. This is why many browsers don't support this anymore, until some patch removes the threat for users.
Standard message solution:
window.addEventListener('beforeunload', function (e) {
// Cancel the event
e.preventDefault();
// Chrome requires returnValue to be set
e.returnValue = '';
});
Look at Ouibounce it helps detect when a user is about to leave the page(It looks at the position of the cursor). You could probably build off this library.