我需要在用户离开页面之前警告用户未保存的更改(这是一个非常常见的问题).
window.onbeforeunload=handler
这有效,但它会引发一个默认对话框,其中包含一条包含我自己文本的令人生气的标准消息.我需要完全替换标准消息,所以我的文本是清楚的,或者(更好)用一个模式对话框替换整个对话框使用jQuery.
到目前为止,我失败了,我还没有找到任何似乎有答案的人.它甚至可能吗?
我的页面中的Javascript:
<script type="text/javascript">   
   window.onbeforeunload=closeIt;
</script>
closeIt()函数:
function closeIt()
{
  if (changes == "true" || files == "true")
  {
      return "Here you can append a custom message to the default dialog.";
  }
}
使用jQuery和jqModal我尝试过这种事情(使用自定义确认对话框):
$(window).beforeunload(function() {
        confirm('new message: ' + this.href + ' !', this.href);
        return false;
    });
这也行不通 - 我似乎无法绑定到beforeunload事件.
在stackoverflow中,如果您开始进行更改,那么您尝试离开页面,会出现一个javascript确认按钮并询问:"您确定要离开此页面吗?" blee blah bloo ...
有没有人以前实现过这个,我如何跟踪提交的更改?我相信我自己可以做到这一点,我正在努力学习专家们的良好做法.
我尝试了以下但仍然无法正常工作:
<html>
<body>
    <p>Close the page to trigger the onunload event.</p>
    <script type="text/javascript">
        var changes = false;        
        window.onbeforeunload = function() {
            if (changes)
            {
                var message = "Are you sure you want to navigate away from this page?\n\nYou have started writing or editing a post.\n\nPress OK to continue or Cancel to stay on the current page.";
                if (confirm(message)) return true;
                else return false;
            }
        }
    </script>
    <input type='text' onchange='changes=true;'> </input>
</body>
</html>
任何人都可以发一个例子吗
有谁知道onbeforeunloadiPad上是否支持该事件和/或是否有不同的使用方式?
我已经尝试了很多东西,似乎onbeforeunload事件永远不会在iPad(Safari浏览器)上触发.
具体来说,这就是我尝试过的:
window.onbeforeunload = function(event) { event.returnValue = 'test'; }window.onbeforeunload = function(event) { return 'test'; }window.onbeforeunload = function(event) { alert('test')'; }<body onbeforeunload="..."> 所有这些都适用于PC上的FF和Safari,但不适用于iPad.
此外,我在加载页面后完成了以下操作:
alert('onbeforeunload' in window);
alert(typeof window.onbeforeunload);
alert(window.onbeforeunload);
结果分别是:
trueobjectnull因此,浏览器确实具有该属性,但由于某种原因它不会被触发.
我尝试离开页面的方法是单击后退和前进按钮,在顶部栏中进行谷歌搜索,更改地址栏中的位置,以及单击书签.
有没有人知道发生了什么?我非常感谢任何意见.
谢谢
我有一个小问题.我试图抓住窗口的OnUnLoad事件并询问确认问题,如果用户决定他们想要保持那么好,如果他们想离开页面,那么他们将丢失所有未保存的数据.这是问题......
我正在使用jQuery UI对话框,当我在页面上放置以下代码时,我打开了Dialog,当我单击浏览器上的后退按钮时,它永远不会弹出msgbox.它只刷新页面:
<script type="text/javascript"> 
    $(window).bind('beforeunload', function() { 
            alert('you are an idiot!'); 
        } 
    );
</script>
我正在使用的解决方案是这里的帖子.再次,如果我没有打开jQuery UI对话框,msgbox将显示正常.如果我这样做,那么它不会显示msgbox并只刷新页面.
有任何想法吗?
我尝试使用以下代码在关闭浏览器窗口时收到警报:
window.onbeforeunload = confirmExit;
function confirmExit() {
  return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
}
它有效,但如果页面包含一个超链接,则单击该超链接会引发相同的警报.我只有在关闭浏览器窗口而不是单击超链接时才需要显示警报.
看起来Apple已禁用iOS设备(iPhone,iPad,iPod Touch)的window.onbeforeunload事件.很遗憾,我找不到任何关于此事件在Mobile Safari中无效的文档.
有谁知道这个功能有可靠的替代方案吗?Android的浏览器似乎支持它很好,Safari桌面应用程序也支持onbeforeunload事件没有问题.
是否有另一种方法可以在mobile-safari中使用而不是onbeforeunload?我注意到Google能够onbeforeunload在移动游戏中捕获这个事件.有没有人想出他们是如何做到的?
谷歌能够使用他们的Gmail客户端这样做.创建一条新消息...在文本区域中键入内容...点击浏览器后退按钮.它弹出一条警告信息.我用iPad做了我的测试.
难道window.onbeforeunload()火在所有浏览器?我需要一个至少由IE6和FF3.6支持的onbeforeunload功能.
对于IE,onbeforeunload()似乎只有IE9支持
嗨,我想自定义弹出窗口,是否有任何简单的方法可以做到这一点?
我使用的是简单的jQuery
$(document).ready(function() {
    var myPopUp = $('#pop-up').css('display', 'block');
    $(window).bind('beforeunload', function(){
      return 'load my pop up here instead of the default browser message';
    });
});
我弹出的html是默认隐藏的
<div id="pop-up">
  <h2>Are you sure wanna leave</h2>
  <a href="#">Leave</a>
  <a href="#" class="close">Stay</a>
</div>
谢谢