1

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

<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>
4

1 回答 1

0

您不能通过使用 javascript 代码来限制用户,如果用户在浏览器设置中禁用了 javascript 怎么办。

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

当用户注册/登录时,在数据库的 login_session 表中插入一个带有时间和日期的新条目,在登录之前检查条目是否存在,如果存在,则在他上次登录时过去了多长时间,如果上次登录时间超过 5 分钟或无论您认为适合自己的时间,更新条目并创建新会话。

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

于 2013-04-01T07:38:45.673 回答