简体   繁体   English

完全禁用浏览器的后退按钮

[英]Disable browsers' back button, completely

Need to prevent users going to the previous page, completely. 需要防止用户完全转到上一页。

When I use the following code it works but it's not what I need exactly. 当我使用以下代码时,它可以工作,但这不是我真正需要的。 When pressing the back button it says "Document Expired": 当按下后退按钮时,它显示“ Document Expired”:

Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
Response.Cache.SetValidUntilExpires(false);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

Another idea - to open a new window without toolbar: 另一个想法-打开一个没有工具栏的新窗口:

<script>
    function PopupWithoutToolbar(link) {
        var w = window.open(link.href,
            link.target || "_blank",
            'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,dependent,width=800,height=620,left=0,top=0');
        return w ? false : true;
    }
</script>

<a href="http://www.google.com" onclick="return PopupWithoutToolbar(this)">yahoo</a>

But, still... If the user presses the backspace button on a keyboard he can go back. 但是,仍然...如果用户按下键盘上的退格按钮,他可以返回。 It seems that this approach is only for hiding and not disabling buttons. 看来这种方法仅用于隐藏而不是禁用按钮。

Is there any way to simply ignore the back button? 有什么方法可以简单地忽略后退按钮吗?

I am not entirely sure if this will work, but you can try handling the event with javascript. 我不确定这是否行得通,但是您可以尝试使用javascript处理事件。

Like if you want to entirely disable the backspace button from allowing users to go back you can do like 就像您想完全禁止退格按钮不允许用户返回一样,您可以像

$(window).on("keypress", function (e){
    if(e.keycode == "backspace") 
         e.preventDefault();
})

I could figure out the keycode for backspace for you , but that isn't too hard to figure out. 我可以为您找出退格键,但这并不太难。 Also this uses jquery, but you can use just raw javascript. 同样,这使用jquery,但您只能使用原始javascript。 just wasn't sure what it would be offhand. 只是不确定会发生什么。

I'm using a slightly different solution: 我使用的解决方案略有不同:

history.pushState(null, null, location.href);
window.onpopstate = function () {
    history.go(1);
}

Simplest thing ever: 最简单的事情:

window.onhashchange = function (event) {
 //blah blah blah
 event.preventDefault();
 return false;
}

You can handle the location domain etc from that (window.location) then cancel the event if you want in this case. 您可以从该处处理位置域等(window.location),然后在这种情况下取​​消事件。

How to Detect Browser Back Button event - Cross Browser 如何检测浏览器后退按钮事件-跨浏览器

Based on your post it sounds like your only issue is disabling the backspace button from allowing the user to go back. 根据您的帖子,听起来您唯一的问题是禁用退格按钮以不允许用户返回。

Here's what I do for that using jquery. 这是我使用jquery所做的工作。 Still allows backspace to work inside enabled text editing inputs, where it should. 仍然允许退格键在应启用的文本编辑输入中正常工作。

    // Prevent the backspace key from navigating back.
    $(document).unbind('keydown').bind('keydown', function (event) {
        var doPrevent = false;
        if (event.keyCode === 8) {
            var d = event.srcElement || event.target;
            if ((d.tagName.toUpperCase() === 'INPUT' && (d.type.toUpperCase() === 'TEXT' ||
                                                         d.type.toUpperCase() === 'PASSWORD' ||
                                                         d.type.toUpperCase() === 'FILE')) ||
                                                         d.tagName.toUpperCase() === 'TEXTAREA') {
                doPrevent = d.readOnly || d.disabled;
            }
            else {
                doPrevent = true;
            }
        }

        if (doPrevent) {
            event.preventDefault();
        }
    });

To disable the back button in the browser you can use use the following code in your JavaScript on the page on which you want to disable the back button. 要在浏览器中禁用后退按钮,可以在要禁用后退按钮的页面上的JavaScript中使用以下代码。

<script>
history.pushState(null, null, location.href);
    window.onpopstate = function () {
        history.go(1);
    };
</script>

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

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