Javascript window.history.back() 不起作用。

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

window.history.back() not working.

javascripthtml

提问by Darran Smith

I cant get this to work. I have been trying for ages. Please help me.

我不能让这个工作。我一直在努力多年。请帮我。

<script>
function goBack() {
    window.history.back()
}
</script>

<button onclick="goBack()">Go Back</button> 

回答by giulp

Please have a look at this question: Inconsistency with window.history.back().

请看看这个问题:Inconsistency with window.history.back()

this

这个

<button type="button" onclick="goBack()">Go Back</button> 

could be what you're looking for

可能是你要找的

As Kevin B suggests

正如凯文 B 所建议的那样

The browser could be interpreting the button as a submit button and submitting the form, thus causing a page refresh. Adding type="button" will prevent that.

浏览器可能会将按钮解释为提交按钮并提交表单,从而导致页面刷新。添加 type="button" 将阻止这种情况。

回答by Logicgate

First of all, you should make sure that your script tag have the proper type set:

首先,您应该确保您的脚本标签具有正确的类型集:

<script type="text/javascript">
...
</script>

Also, I would suggest using the "go" function of the history object instead since the compatibility is higher. To simplify things you can simply do this:

另外,我建议改用历史对象的“go”函数,因为兼容性更高。为了简化事情,您可以简单地执行以下操作:

<button onclick="javascript:history.go(-1)">Go Back</button>

Hope this helps.

希望这可以帮助。

回答by user10056111

This worked for me

这对我有用

    <button type="reset" onclick="goBack()">Go Back</button> 

回答by hev1

Try preventing the default action of the button. Note that nothing will happen if you have accessed no other pages in the current tab.

尝试阻止按钮的默认操作。请注意,如果您没有访问当前选项卡中的其他页面,则不会发生任何事情。

Javascript:

Javascript:

<button id="goBack">Go Back</button>
<span id="result"></span>
<script>
document.getElementById("goBack").addEventListener("click", function(event){
event.preventDefault();
document.getElementById("result").innerHTML = "Going back";
window.history.back();
});
</script>

jQuery:

jQuery:

<button id="goBack">Go Back</button>
<span id="result"></span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$('#goBack').click(function(event){
event.preventDefault();
document.getElementById("result").innerHTML = "Going back";
window.history.back();
});
</script>

回答by Trisha Amable

The window.history.back()method does not work if it does not have any previous URL to go to. Your program needs to have a start URL and you will need to have your program moving forward to the next page to be able to go back and forward.

window.history.back()如果该方法没有任何可访问的先前 URL,则该方法不起作用。您的程序需要有一个起始 URL,并且您需要让您的程序前进到下一页才能返回和前进。

回答by tony

This can stop it appearing to work

这可以阻止它似乎工作

<a href="#" onclick=DoSomething()>Stop it working</a>

That's because the href will effectively add a new page in the history, the present page again

那是因为 href 将有效地在历史记录中添加一个新页面,再次添加当前页面

Incidently if you call

顺便说一下,如果你打电话

event.preventDefault()

in DoSomething then # will never get added to the history and it will appear to work again

在 DoSomething 然后 # 永远不会被添加到历史记录中,它似乎会再次工作