如何检测浏览器窗口/选项卡closures事件?

我尝试与onbeforeunload和卸载function。 但它没有工作。 点击链接或刷新时,此事件被触发。 我想要一个只有当浏览器窗口或标签closures时才触发的事件。 该代码必须在所有浏览器中工作。

我在Masterpage中使用以下代码。

<script type="text/jscript"> var leaving = true; var isClose = false; function EndChatSession() { var request = GetRequest(); request.open("GET", "../Chat.aspx", true); request.send(); } document.onkeydown = checkKeycode function checkKeycode(e) { var keycode; if (window.event) keycode = window.event.keyCode; else if (e) keycode = e.which; if (keycode == 116) { isClose = true; } } function somefunction() { isClose = true; } window.onbeforeunload = function (e) { if (!e) e = event; if (leaving) { EndChatSession(); e.returnValue = "Are You Sure"; } } function GetRequest() { var xmlHttp = null; try { // Firefox, Opera 8.0+, Safari xmlHttp = new XMLHttpRequest(); } catch (e) { //Internet Explorer try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } </script> 

并在我的身体标记:

上面的代码在IE中工作,但不是在铬…

此代码可以防止checkbox事件。 它在用户点击浏览器closuresbutton时起作用,但在点击checkbox时不起作用。 你可以修改它的其他控件(texbox,单选button等)

  window.onbeforeunload = function () { return "Are you sure?"; } $(function () { $('input[type="checkbox"]').click(function () { window.onbeforeunload = function () { }; }); }); 

你无法用javascript检测到它。

只有检测到页面卸载/closures的事件是window.onbeforeunload和window.unload 。 这些事件都不能告诉你closures页面的方式。

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> <script type="text/javascript"> var validNavigation = false; function wireUpEvents() { var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation var leave_message = 'ServerThemes.Net Recommend BEST WEB HOSTING at new tab window. Good things will come to you' function goodbye(e) { if (!validNavigation) { window.open("http://serverthemes.net/best-web-hosting-services","_blank"); return leave_message; } } window.onbeforeunload=goodbye; // Attach the event keypress to exclude the F5 refresh $(document).bind('keypress', function(e) { if (e.keyCode == 116){ validNavigation = true; } }); // Attach the event click for all links in the page $("a").bind("click", function() { validNavigation = true; }); // Attach the event submit for all forms in the page $("form").bind("submit", function() { validNavigation = true; }); // Attach the event click for all inputs in the page $("input[type=submit]").bind("click", function() { validNavigation = true; }); } // Wire up the events as soon as the DOM tree is ready $(document).ready(function() { wireUpEvents(); }); </script> 

我没有回答你可以使用JavaScript来检测用户是否closures了浏览器选项卡? closures浏览器? 还是已经离开浏览器?

你也可以像下面这样做。

将下面的脚本代码放在标题标签中

 <script type="text/javascript" language="text/javascript"> function handleBrowserCloseButton(event) { if (($(window).width() - window.event.clientX) < 35 && window.event.clientY < 0) { //Call method by Ajax call alert('Browser close button clicked'); } } </script> 

从下面的body标签调用上面的函数

 <body onbeforeunload="handleBrowserCloseButton(event);"> 

谢谢

就在这里! 经过很多头痛,我find了一个解决办法。

Monitor.php

这个php文件将监视浏览器closures事件。 一旦浏览器closures,connection_aborted将返回1,因此循环会中断。

 <?php // Ignore user aborts and allow the script // to run forever ignore_user_abort(true); set_time_limit(0); echo connection_aborted(); while(1) { echo "Whatever you echo here wont be printed anywhere but it is required in order to work."; flush(); if(connection_aborted()) { break; // Breaks only when browser is closed } } /* Action you want to take after browser is closed. Write your code here */ ?> 

Caller.php

这是将调用Monitor.php文件

 <?php Header('Location: monitor.php'); ?> 

Parent.html

这将是你将实际交互的文件。 在加载时,会直接调用一个到Caller.php的AJAX调用,它将在后台模式下自动启动Monitor.php。

 <script> var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { } } xmlhttp.open("GET", "Caller.php", true); xmlhttp.send(); </script> 

所以最后的stream程是Parent.html —–> Caller.php —–> Monitor.php

刷新和closures选项卡或导航器之间的差异,这里是我如何解决它:

 <script> function endSession() { // Browser or Broswer tab is closed // Write code here } </script> <body onpagehide="endSession();"> </body>