【问题标题】:Stop URL redirect to a particular URL using JS/JQuery [duplicate]使用 JS/JQuery 停止 URL 重定向到特定 URL [重复]
【发布时间】:2019-11-16 04:51:32
【问题描述】:

我需要停止重定向到特定的 url。有一段加密的异步 js 代码在添加任何外部/第 3 方 js 时重定向到另一个页面。是否有可能有一个事件或任何东西可以单独检测到该特定网址的重定向? 我用过这段代码

var back = false;
back = true; //Somewhere, the condition is set to true
window.onbeforeunload = function (e) {
    if(back == true)
        return "Are you sure to exit?";
}

弹出后可以自动取消吗?

【问题讨论】:

  • 添加更多详细信息,以及您尝试过的代码。
  • 它是一个被缩小的博客模板代码。我正在尝试添加反广告拦截器脚本。一旦广告拦截器激活,它就会重定向到模板网站。
  • @bob 当然有帮助... 就是这样,每次警报出现时是否可以自动单击取消?
  • 我不知道。我尝试了各种方法,我会在下面的答案中告诉你。
  • 感谢大家的回答.. 但是当有人有问题时为什么要投反对票!!!!

标签: javascript jquery redirect


【解决方案1】:

您可以阻止页面导航离开,但始终会触发一个对话框,询问用户是否要离开。

我认为这是浏览器强制执行的“人机交互”之一,因为此功能可能会导致浏览器永远卡在一个网页/站点上。

window.addEventListener("beforeunload", function(evt){

   // Didn't see anything in the event that eluded to the intended URL to navigate to.
   // So filtering the navigation based on the destination seems impossible?
   for(var prop in evt){
       console.log(prop, evt[prop]);
   }
   evt.preventDefault(); // prevents navigation (which is the default action)
   evt.returnValue = true; // for chrome

}, true); // "use capture" = true

【讨论】:

    猜你喜欢
    • 2017-09-22
    • 2018-02-15
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 2015-10-28
    • 2018-12-15
    相关资源
    最近更新 更多