javascript 在某些页面上禁用“history.go(-1)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4070835/
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-10-25 10:19:09  来源:igfitidea点击:

Disable "history.go(-1)" on certain pages

javascript

提问by Desmond Liang

I want to be able to disable "history.go(-1)" on certain pages. I'm thinking if is there a way to clean up the Javascript's history object. Ideas? "history.go()" seen to be not functioning too well on certain browsers such as IE. Is that true? Any solution for that?

我希望能够在某些页面上禁用“history.go(-1)”。我在想是否有办法清理 Javascript 的历史对象。想法?“history.go()”在某些浏览器(例如 IE)上运行得不太好。真的吗?有什么解决办法吗?

回答by Robusto

The best you can do to "disable" history is to open your page in a new window, which will have no history. Pages opened in a new window don't have a back button enabled and history.go(-1) will not do anything

“禁用”历史记录的最佳方法是在没有历史记录的新窗口中打开您的页面。在新窗口中打开的页面没有启用后退按钮并且 history.go(-1) 不会做任何事情

Short of that, there is nothing you would (or should) be able to do.

除此之外,您将(或应该)无能为力。

Revised to make an implication explicit. I thought I was being clear, but apparently I was not.

修订以明确含义。我以为我很清楚,但显然我不是。

回答by Peter Bailey

You can over-write the gomethod, but it doesn't get you much

您可以覆盖该go方法,但它不会给您带来太多好处

history.go = function(){};

This will disable all uses of history.go, and not just when you pass a -1.

这将禁用 的所有使用history.go,而不仅仅是在您通过-1.

And as others have pointed out - this doesn't stop the user from using the Back button on their browser.

正如其他人指出的那样 - 这并不能阻止用户使用浏览器上的后退按钮。

回答by Diodeus - James MacFarlane

You cannot override this browser behavior. If you could there would be mayhem as malware would try to take over everyone's history.

您无法覆盖此浏览器行为。如果可以,将会造成混乱,因为恶意软件会试图接管每个人的历史。

回答by m.edmondson

If you're trying to disable the browsers BACK button this is simply not possible. Would you like it if websites could mess around with your browsers functionality?

如果您试图禁用浏览器的后退按钮,这是不可能的。如果网站可能会干扰您的浏览器功能,您会喜欢吗?

回答by Teson

You've got two options.

你有两个选择。

  1. Load pages with post request, this will prevent users from using back-button as you can trap the reload on server-side. (You need to wrap all hyperlinks with js though, so a bit of work, and the solution is quite ugly.)

  2. Use DOM to load content into page. History won't be able to backtrack it. (or am i wrong here :-| )

  3. describe your problem more clearly, there's probably better solutions for where you need to go.

  1. 使用 post 请求加载页面,这将阻止用户使用后退按钮,因为您可以在服务器端捕获重新加载。(虽然你需要用js包装所有的超链接,所以有点工作,而且解决方案很丑陋。)

  2. 使用 DOM 将内容加载到页面中。历史将无法追溯。(或者我在这里错了:-|)

  3. 更清楚地描述您的问题,您需要去的地方可能有更好的解决方案。

回答by Jim Fell

Put this in your shared page header:

把它放在你的共享页眉中:

<body onLoad="history.forward()">

It will always force the user to the most recent page they've visited on your website.

它总是会强制用户访问他们在您网站上访问过的最新页面。

回答by Rodrigo

This is ugly, but might work:

这很丑陋,但可能有效:

$(document).load(function(){   
   document.location.href = document.location.href + "#";   
   setInterval(function(){   
      if (window.location.hash != '#')   
         document.location.href = document.location.href + "#";   
   }, 100);   
});  

ugh!

啊!

回答by Teson

Desmond,

德斯蒙德

My boss doesn't want the back button to work on the landing page.I know it's stupid but what can I do.

我的老板不想让后退按钮在登陆页面上工作。我知道这很愚蠢,但我能做什么。

What about using default page, (index.php or whatever) to point forward.

使用默认页面(index.php 或其他)指向前方怎么样。

index.php:

索引.php:

header('location:start.html');
//or likewise with js,
document.location = 'start.html';

start.html:

开始.html:

my real start

as user hits the back-button, the index.php will redo the forwarding.

当用户点击后退按钮时,index.php 将重做转发。

regards,

问候,

回答by Desmond Liang

Thanks for all the responses. After two days of juggling, I came up this solution, for now. I use PHP to output a class to the body tag if it's the current page is the front page. Something like

感谢所有的回应。经过两天的折腾,我暂时想到了这个解决方案。如果当前页面是首页,我使用 PHP 将一个类输出到 body 标记。就像是

<body class='front'>

and then use jQuery to enable/disable "history.go(X)" on the fly.

然后使用 jQuery 动态启用/禁用“history.go(X)”。

$(document).ready(){
    if($('body.front').length>0){
        $('#backbutton').click(function(){ window.history.back(); }); 
    }else{
        $('#backbutton').unbind('click');
    }
} 

In fact the project that I was working on require the site inside an iframe to disable the element that triggers the back button of the parent window. The js becomes:

事实上,我正在处理的项目要求 iframe 内的站点禁用触发父窗口后退按钮的元素。js变成了:

var backbutton=parent.window.getElementById('#backbutton');
$(document).ready(){
    if($('body.front').length>0){
        $(backbutton).click(function(){ window.history.back(); }); 
    }else{
        $(backbutton).unbind('click');
    }
}