【问题标题】:onbeforeunload not workingonbeforeunload 不工作
【发布时间】:2014-12-23 08:57:50
【问题描述】:

下面是我正在使用的代码 sn-p 但从未调用过 onbeforeunload 事件处理程序。

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]>      <html class="no-js"> <![endif]-->

<head runat="server">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Submit</title>
<meta name="keywords" content="" />
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link id="Link1" runat="server" rel="shortcut icon" href="~/favicon.ico" type="image/x-icon" />
<link id="Link2" runat="server" rel="icon" href="~/favicon.ico" type="image/ico" />
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
    <%: Scripts.Render("~/bundles/modernizr") %>
</asp:PlaceHolder>

<link rel="stylesheet" href="/Content/themes/ab1.css" media="screen">
<link rel="stylesheet" href="/Content/themes/ab2.css" />
<link rel="stylesheet" href="/Content/themes/ab3.css">
<link rel="stylesheet" href="/Content/themes/ab4.css">
<!-- LayerSlider styles -->
<link rel="stylesheet" href="/Content/themes/ab5.css">
<link rel="stylesheet" href="/Content/themes/ab6.css" id="coloroption">
<link rel="stylesheet" href="/Content/themes/ab7.css">
<link rel="stylesheet" type="text/css" media="screen" href="/Content/themes/ab8.css">
<link rel="stylesheet" href="/Content/themes/ab9.css" id="styleswitcher">

<script src="/Content/themes/Scripts/modernizr-2.6.2-respond-1.1.0.min.js"></script>
<script src="/Content/themes/Scripts/jquery-1.10.2.min.js"></script>
<script src="/Content/themes/Scripts/jquery-ui.js"></script>
<!-- jQuery with jQuery Easing, and jQuery Transit JS -->
<%--<script src="layerslider/jQuery/jquery-easing-1.3.js"></script>
<script src="layerslider/jQuery/jquery-transit-modified.js"></script>--%>

<!-- LayerSlider from Kreatura Media with Transitions -->

<!--[if IE 7]>
  <link rel="stylesheet" href="css/font-awesome-ie7.min.css">
<![endif]-->
<link rel="stylesheet" type="text/css" href="/Content/themes/Scripts/combobox/dd.css" />
<script src="/Content/themes/Scripts/combobox/jquery.dd.js"></script>
<script type="text/javascript">
    function fnCallUnload() {
        debugger;
        var xmlData = "<Root sMethodName='SaveState'>";
        fnSetCommonDataToXMLVariable(xmlData);
        xmlData += "</Root>";

        CallServer(xmlData, "");
    }

    window.onbeforeunload = fnCallUnload;
</script>
</head>

我在母版页中尝试了 onbeforeunload 事件,并在 try catch 中调用了内容页的函数。它在那里工作顺利。但是我尝试使用 onbeforeunload 的这个页面不需要母版页。它是应用程序中的一个独立页面。所以,我想关于 onbeforeunload 的使用有一些我不知道的小概念。

我们将不胜感激。

谢谢

【问题讨论】:

  • 在页面周期中甚至考虑调用服务器都为时已晚,在用户点击后退按钮或关闭标签或关闭浏览器甚至计算机之前很久就这样做
  • @charlietfl 我以前做过。在 onbeforeunload 中调用 javascript 函数,并使用回调功能调用 RaiseCallbackEvent 函数,它确实有效。我很困惑为什么它在这里不起作用。

标签: javascript html onbeforeunload


【解决方案1】:

这应该可以,但是 unbeforeunload 需要返回一个字符串。否则,出于安全原因,它不会触发。

将此添加为最后一行。

return "Do you want to quit?";

然而,这个帖子将 jQuery 归咎于罪魁祸首 window.onbeforeunload not firing

改变

window.onbeforeunload = fnCallUnload;

window.addEventListener("beforeunload", fnCallUnload, false);

【讨论】:

  • 有趣的是,在母版页存在且母版页包含相同数量的 JQuery 的其他页面中,同样的事情。
  • @user2861226 确实很有趣。查看代码编辑。
  • 看起来可以工作,但是在添加这行代码时出现了一些与 ajaxtoolkit 相关的 js 错误。无论如何,谢谢,我会调查并发布这些js错误的原因,然后将您的帖子标记为答案
猜你喜欢
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多