【问题标题】:is it possible to write onbeforeunload like this?可以这样写onbeforeunload吗?
【发布时间】:2013-11-14 07:51:44
【问题描述】:

可以这样写onbeforeunload函数吗?

   window.onbeforeunload = function(evt) { 

            if(return true)
            {
                if(confirm('Are you sure you want to logout'));
                {
                     //logout function
                }
            }
    }

如果用户单击“离开此页面”按钮,我想要求用户注销会话。 有可能吗?

【问题讨论】:

  • 如果返回 true 会怎样?只需返回Are you sure you want to logout
  • 应该是confirm 而不是confitm
  • @putvande 谢谢,我编辑了。
  • @ArunKillu 返回消息在 Firefox 浏览器上不起作用

标签: javascript browser onbeforeunload


【解决方案1】:

这是你需要的吗?

   <script type="text/javascript">
      window.onbeforeunload = function() { 
       if(confirm('Are you sure you want to logout'))
       {
            alert("logout")
       }
    }
  </script>

【讨论】:

  • 您不能在 onbeforeunload 中执行此操作。您需要返回一个字符串,并且不允许使用警报/确认。
  • @putvande 尝试使用代码 sn-p 并告诉。这对我有用
  • @Gowsikan 它正在工作。如果我点击浏览器后退按钮,如果我在确认框中点击取消,它应该保持在同一页面上。但它导航到上一页。有任何选项可以让用户留在同一页面上
  • 谢谢大家,我只是禁用了浏览器的后退按钮。我提到了这个链接,(programmerslate.blogspot.in/2013/06/…)。但我不知道所有浏览器都支持这个...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 2012-02-19
  • 2017-11-30
  • 2020-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多