Javascript 使用JS/JQuery停止URL重定向到特定URL

Javascript 使用JS/JQuery停止URL重定向到特定URL,javascript,jquery,redirect,Javascript,Jquery,Redirect,我需要停止重定向到特定的url。有一段加密的异步js代码,在添加任何外部/第三方js时重定向到另一个页面。是否可能有一个事件或任何可以单独检测重定向到该特定url的内容? 我用过这个代码 var back = false; back = true; //Somewhere, the condition is set to true window.onbeforeunload = function (e) { if(back == true) return "Are you

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

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?";
}

弹出后是否可以自动按cancel?

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

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

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

添加更多详细信息和您尝试过的代码。这是一个已缩小的博客模板代码。我正在尝试添加反adblocker脚本。广告拦截器激活后,它将重定向到模板网站。@bob确实有帮助。。。只是,有可能在每次警报出现时自动点击取消吗?我不知道。我尝试了各种方法,我会在下面的答案中告诉你。谢谢你们的答案。。但是当有人有问题时,为什么要投否决票呢!!!!