Onclick javascript 让浏览器返回上一页?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8067510/
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
Onclick javascript to make browser go back to previous page?
提问by Doc Holiday
Is there a function I can attach as a click event of a button to make the browser go back to previous page?
是否有一个功能可以作为按钮的点击事件附加,使浏览器返回上一页?
<input name="action" type="submit" value="Cancel"/>
回答by rogerlsmith
Add this in your input element
将此添加到您的输入元素中
<input
action="action"
onclick="window.history.go(-1); return false;"
type="submit"
value="Cancel"
/>
回答by Vadim
history.back()
or
或者
history.go(-1)
Put this to the button onclick
handle. It should look like this:
把它放在按钮onclick
手柄上。它应该是这样的:
<input name="action" onclick="history.back()" type="submit" value="Cancel"/>
回答by Rizwan Gill
For Going to previous page
前往上一页
First Method
第一种方法
<a href="javascript: history.go(-1)">Go Back</a>
Second Method
第二种方法
<a href="##" onClick="history.go(-1); return false;">Go back</a>
if we want to more than one step back then increase
如果我们想后退不止一步,那么增加
For going 2 steps back history.go(-2)
For going 3 steps back history.go(-3)
For going 4 steps back history.go(-4)
and so on.......
回答by hspain
<input name="action" type="submit" value="Cancel" onclick="window.history.back();"/>
回答by Rich O'Kelly
You just need to call the following:
您只需要调用以下内容:
history.go(-1);
回答by Michael Durrant
Shortest Yet!
最短的!
<button onclick="history.go(-1);">Go back</button>
I prefer the .go(-number)
method as then, for 1 or many 'backs' there's only 1 method to use/remember/update/search for, etc.
我更喜欢这种.go(-number)
方法,对于 1 个或多个“背”,只有一种方法可以使用/记住/更新/搜索等。
Also, using a tag for a back button seems more appropriate than tags with names and types...
此外,使用标签作为后退按钮似乎比带有名称和类型的标签更合适......
回答by Malik Khalil
window.history.back();
window.history.back();
<button onclick="goBack()">Go Back</button>
<script>
function goBack() {
window.history.back();
}
</script>
回答by Andre Mesquita
Simple. One line.
简单的。一条线。
<button onclick="javascript:window.history.back();">Go Back</button>
Like Wim's and Malik's answer, but just in one line.
就像 Wim 和 Malik 的回答一样,但只是在一行中。
回答by Mannu saraswat
Works for me everytime
每次都对我有用
<a href="javascript:history.go(-1)">
<button type="button">
Back
</button>
</a>
回答by Wim Mostrey
This is the only thing that works on all current browsers:
这是唯一适用于所有当前浏览器的方法:
<script>
function goBack() {
history.go(-1);
}
</script>
<button onclick="goBack()">Go Back</button>