douluolan9101 2013-04-01 07:30
浏览 39

如何检测浏览器/标签已关闭[重复]

This question already has an answer here:

i want to alert box when browser is closed or browser tab is closed. I had some code but in this 1 extra message is coming.. I want to know from where it is coming in alert box. if any other code you have then send me or tell me what should i do.. actually i want to avoid second user login (For this i create 1 session id and save in DB after that every time when user login it check session id is in DB or not if then user can login, when user log out session id is also deleted but browser or tab is closed then session id is not deleted. for this i want to detect that one after that i want to send the control to ajax controller ) that's why i want to know from where 1 extra message is coming. please solve my problem ..

<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>
</div>
  • 写回答

1条回答 默认 最新

  • duandeng2011 2013-04-01 07:38
    关注

    You can't restrict user by using the javascript code, what if user has disabled the javascript in browser settings.

    i think you should handle it using PHP and Database (mysql)

    As user register/login insert a new entry in login_session table in database with time and date, before login check if entry exists or not and if exists how much time it has past in his last login, if last login is more then 5 mints or whatever time you think suits you, update the entry and create new sessions.

    check this similar post here PHP /SESSION: Login one per user?

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站