简体   繁体   中英

onbeforeunload not working

Below is the code snippet that I am using but onbeforeunload event handler is never called.

<!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. It does work smoothly there. But this page where I am trying to use onbeforeunload does not require master page. 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.

Any help will be hugely appreciated.

Thanks

This should work, however unbeforeunload requires a string to be returned. 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

change

window.onbeforeunload = fnCallUnload;

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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