【问题标题】:Can I use onbeforeunload only when the site is being redirected to a particular website [duplicate]只有当网站被重定向到特定网站时,我才能使用 onbeforeunload [重复]
【发布时间】:2019-07-15 14:31:04
【问题描述】:

我只想在网站被重定向到特定页面时使用 onbeforeunload。我该怎么做?

【问题讨论】:

  • 仅当重定向是由用户事件(例如单击链接)或您自己的代码完成时。是这样吗?
  • @charlietfl 是的。

标签: javascript jquery


【解决方案1】:

您可以在单击具有相应 href 的链接时设置全局变量标志。

var doUnload = false;

$(window).on('beforeunload', function(){
   if(doUnload){
     // whatever it is you want to do
   }
});

$(window).on('click', 'a', function(){
     // parse href for match
     if(this.pathname === '/foo/bar/'){
         doUnload = true;
     }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-30
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多