Javascript 如何停止页面重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2635370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:14:50  来源:igfitidea点击:

how to stop page redirect

javascriptjquery

提问by santose

I need to stop all the page redirection through javascript.

我需要通过javascript停止所有页面重定向。

I have some script which will redirect the page to some other location. How can I stop all the page redirection on my page through jquery or javascript.

我有一些脚本可以将页面重定向到其他位置。如何通过 jquery 或 javascript 停止我页面上的所有页面重定向。

回答by Nalum

You can stop the redirect by doing the following.

您可以通过执行以下操作来停止重定向。

<script language="JavaScript" type="text/javascript">
    //<![CDATA[
        window.onbeforeunload = function(){
            return 'Are you sure you want to leave?';
        };
    //]]>
</script>

Not sure if all browsers support this but that should do the trick.

不确定是否所有浏览器都支持这一点,但这应该可以解决问题。

回答by LiverpoolsNumber9

If I'm understanding you correctly, you need to stop users navigating away from the page?

如果我对您的理解正确,您是否需要阻止用户离开页面?

If so, Marcos Placona's answer is one part of it - although all what you actually need is:

如果是这样,Marcos Placona 的答案就是其中的一部分——尽管您真正需要的是:

$("a").click(function(e) { e.preventDefault(); });

You also don't want people to hit F5 I'm guessing? Well that's harder. Preventing defaults on key press, cross-browser is horrible. But a couple of codes that work in some browser are below here:

你也不希望人们按 F5 我猜?嗯,这更难。防止按键默认,跨浏览器是可怕的。但是在某些浏览器中工作的一些代码如下:

function disableF5() {  // IE
    document.onkeydown = function() {
        if (window.event && window.event.keyCode == 116) { // Capture and remap F5
            window.event.keyCode = 505;
        }
        if (window.event && window.event.keyCode == 505) { // New action for F5
        return false; // Must return false or the browser will refresh anyway
        }
    }
}
function disableF5v2() { // FIREFOX
    $(this).keypress(function(e) {
        if (e.keyCode == 116) {
            e.preventDefault();
    return false;
        }
    });
}

However, the cross-browser issue can partly be solved with -

但是,跨浏览器问题可以部分解决 -

window.onbeforeunload = function(e) {
    var message = "Your confirmation message goes here.", e = e || window.event;
    // For IE and Firefox 
if (e) {
    e.returnValue = message;
}
// For Safari
    return message;
};

Hope that all helps anyway. The last bit of code is from another question on this site here. Rob

无论如何,希望一切都会有所帮助。代码的最后一位是从这个网站的其他问题在这里。抢

回答by Peeter

Are you trying to stop an iframe breaker? Something like:

您是否正在尝试阻止 iframe 断路器?就像是:

<script language="JavaScript" type="text/javascript">
<!--
function breakout_of_frame()
{
  if (top.location != location) {
    top.location.href = document.location.href ;
  }
}
-->
</script>

If so, you can not.

如果是这样,你不能。

回答by Marcos Placona

If it's only redirects through link clicks, in jQuery you can do this:

如果它只是通过链接点击重定向,在 jQuery 中你可以这样做:

$(".someClass a").unbind('click');

if it's an existing page redirect, you'll need to give us some more information about what kind of redirect this is, so we can help further.

如果它是现有的页面重定向,您需要向我们提供有关重定向类型的更多信息,以便我们进一步提供帮助。

Hope this helps you

希望这对你有帮助

回答by rahul

AFAIK this won't be possible, since page redirection can occur in many ways. He can click on an anchor tag, can submit a form with different action, set window.location to another page on button click and invoke server code to redirection.

AFAIK 这是不可能的,因为页面重定向可以通过多种方式发生。他可以单击锚标记,可以提交具有不同操作的表单,在单击按钮时将 window.location 设置为另一个页面并调用服务器代码进行重定向。

And you won't be able to detect many of these.

你将无法检测到其中的许多。