admin管理员组文章数量:1390547
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.
本文标签: javascriptConfirmation before leavingclosing of tabStack Overflow
版权声明:本文标题:javascript - Confirmation before leavingclosing of tab? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744604248a2615256.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论