当用户尝试使用jquery脚本使用f5,ctrl + 5刷新浏览器页面时,警报不起作用 [英] Alert is not working when user try to refresh browser page using f5,ctrl+5 using jquery script
问题描述
当用户尝试在浏览器中引用选项卡时,我需要向用户显示警报,它应该会收到警报如果您警告此页面,您将丢失数据",但是我正在为此使用jquery,但是它仍然无法正常工作.
I have requirement of show alert to the user when user try to referesh tab in browser it should get alert "if you alert this page you will loose data" but I am using jquery for this still it is not working .
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(window).on('beforeunload', function(){
alert("Don't refresh you will loose your all data?");
//or
confirm("are you sure want to refresh this page !");
});
</script>
推荐答案
尝试使用return
Try This with return
注意: 并非所有浏览器都支持
Note: NOT all browsers support this
$(window).on("beforeunload", function (event) {
return "Are you sure you want to leave?";
});
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
其他信息:
Google Chrome在版本51中删除了对自定义消息的支持
Google Chrome removed support for custom message in ver 51
Opera在版本38中删除了对自定义消息的支持
Opera removed support for custom message in ver 38
Mozilla Firefox在版本44.0中删除了对自定义消息的支持
Mozilla Firefox removed support for custom message in ver 44.0
Apple Safari在9.1版中删除了对自定义消息的支持
Apple Safari removed support for custom message in ver 9.1
Ref: https://stackoverflow.com/a/38880926/6299088
这篇关于当用户尝试使用jquery脚本使用f5,ctrl + 5刷新浏览器页面时,警报不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!