2013-04-01 166 views
1

我想在浏览器关闭或浏览器选项卡关闭时提醒框。我有一些代码,但在这1额外的消息即将到来..我想知道它从哪里进入警戒框。如果有任何其他代码,然后发送给我或告诉我该怎么办..其实我想避免第二次用户登录(为此,我创建1会话ID并保存在数据库后,每当用户登录时检查会话ID是在数据库或不,如果然后用户可以登录,当用户注销会话ID也被删除,但浏览器或选项卡已关闭,然后会话ID不会被删除为此,我想检测一个之后,我想发送控制ajax控制器)这就是为什么我想知道从哪里来的额外信息。请解决我的问题..如何检测浏览器/选项卡已关闭

<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 = 'You sure you want to leave?' 
    function goodbye(e) { 
    if (!validNavigation) { 
     if (dont_confirm_leave!==1) { 
     if(!e) e = window.event; 
     //e.cancelBubble is supported by IE - this will kill the bubbling process. 
     e.cancelBubble = true; 
     e.returnValue = leave_message; 
     //e.stopPropagation works in Firefox. 
     if (e.stopPropagation) { 
      e.stopPropagation(); 
      e.preventDefault(); 
     } 
     //return works for Chrome and Safari 
     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> 
+0

看到这个[链接](http://eureka.ykyuen.info/2011/02/22/jquery-javascript-capture-the-browser-or-tab-关闭事件/) – 2013-04-01 07:33:26

+0

我想你可以使用onunload事件。 –

+0

如果我使用onload事件,那么每次它发出警报框 – Anurag

回答

0

你不能使用javascript代码,如果有什么用户禁用浏览器设置的JavaScript限制用户。

我认为你应该使用PHP和数据库(MySQL的)处理它

随着用户注册/登录插入login_session表中数据库的时间和日期, 登录前检查新的条目;如果条目存在或不和如果存在多少时间,他最后一次登录时, 如果上次登录超过5个薄荷糖或任何您认为合适的时间,更新条目并创建新的会话。

检查这个类似的帖子在这里 PHP /SESSION: Login one per user?

相关问题