简体   繁体   English

onbeforeunload不起作用

[英]onbeforeunload not working

Below is the code snippet that I am using but onbeforeunload event handler is never called. 下面是我正在使用的代码段,但是从未调用过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>

I tried onbeforeunload event in the master page and calling a function of content page in the try catch. 我在母版页中尝试过onbeforeunload事件,并在try catch中调用了内容页的功能。 It does work smoothly there. 它在那里确实工作正常。 But this page where I am trying to use onbeforeunload does not require master page. 但是我尝试使用onbeforeunload的此页面不需要母版页。 Its a stand alone page in the application. 它是应用程序中的独立页面。 So, I guess there is some minor concept I am not aware of regarding the use of onbeforeunload. 因此,我猜想关于onbeforeunload的使用我没有意识到一些小概念。

Any help will be hugely appreciated. 任何帮助将不胜感激。

Thanks 谢谢

This should work, however unbeforeunload requires a string to be returned. 这应该可以,但是unbeforeunload要求返回一个字符串。 If not it will not fire due to security reasons. 否则,由于安全原因,它将不会着火。

Add this as last line. 将此添加为最后一行。

return "Do you want to quit?";

However this thread blames jQuery as culprit window.onbeforeunload not firing 但是此线程将jQuery归咎于window.onbeforeunload不触发

change 更改

window.onbeforeunload = fnCallUnload;

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM