onbeforeunload无法正常工作

时间:2014-12-23 08:57:51

标签: javascript html onbeforeunload

以下是我正在使用的代码段,但永远不会调用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使用的一些小概念我不知道。

非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:0)

这应该可行,但是unbeforeunload需要返回一个字符串。如果不是,由于安全原因它不会发射。

将此添加为最后一行。

return "Do you want to quit?";

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

变化

window.onbeforeunload = fnCallUnload;

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