简体   繁体   English

JQuery JS - 如何捕获重定向并更改重定向 url?

[英]JQuery JS - How to catch redirect and change the redirect url?

I have a js plugin that redirects the page after successful action.我有一个 js 插件,可以在成功操作后重定向页面。

I want to catch page unload and redirect to my own url.我想捕获页面卸载并重定向到我自己的 url。

I tried using beforeunload event, but I can't use it because it just asks the user if it's ok with him to leave the page.我尝试使用beforeunload事件,但我无法使用它,因为它只是询问用户是否可以离开页面。 Not quite what I'm looking for.不完全是我要找的。

It does not matter what the plugin does, the question is:插件做什么并不重要,问题是:
Is it possible to catch page redirect event, and redirect to another url instead?是否可以捕获页面重定向事件,然后重定向到另一个 url?

If you mean that you want to do this in code in a web page (not a browser extension), you can't.如果您的意思是要在网页(而不是浏览器扩展程序)中的代码中执行此操作,则不能。 If the user wants to close the page or navigate to a different page, there is nothing your JavaScript code in the page they're leaving can do to prevent it.如果用户想要关闭页面或导航到不同的页面,他们离开的页面中的 JavaScript 代码无法阻止它。 It was something scummy websites tried to do early on, which is now appropriately prevented by browsers.这是垃圾网站早期试图做的事情,现在浏览器可以适当地阻止它。


(I'd be surprised if you could do it in a browser extension, either, but they do have more capabilities than normal web pages do.) (如果您也可以在浏览器扩展程序中执行此操作,我会感到惊讶,但它们确实比普通网页具有更多功能。)

Is something like this your intended behaviour?这是您预期的行为吗?

document.onbeforeunload = function (e) {
  var e = e || window.event;
  e.returnValue = "sure?";
}
var test = document.getElementById("reload_test");
  test.onclick = function () {
  document.onbeforeunload = function(){};
}

I'm creating the leaving prompt, but emptying the "onbeforeunload" method when you try to move to another webpage.我正在创建离开提示,但在您尝试移动到另一个网页时清空“onbeforeunload”方法。

JSFiddle seems to work as you intended (could have some issues on some other browsers since I don't know how they handle the "onbeforeunload" event). JSFiddle 似乎按您的预期工作(在其他一些浏览器上可能会有一些问题,因为我不知道它们如何处理“onbeforeunload”事件)。

JSFiddle JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM