Javascript 返回上一页

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

get back to previous page

javascriptjquerybrowser-history

提问by 3gwebtrain

just i want to make back button in my site. once i click the button it need to take the url in to previous page. how can i make this using jquery?

只是我想在我的网站上制作后退按钮。一旦我单击按钮,它需要将 url 带到上一页。我怎样才能使用 jquery 做到这一点?

回答by wRAR

<button type="button" onclick="history.back();">Back</button>

回答by Diego C.

the answer by wRAR is correct, you can use history.back or history.go(-1). However, if you are using ajax, you might need a bit more than that.

wRAR 的答案是正确的,您可以使用 history.back 或 history.go(-1)。但是,如果您使用的是 ajax,则可能需要更多。

Stephen Walter has a nice article on how to use jQuery, ASP.NET, and Browser Historywhich basically describes how to save and restore the application state yourself. I recommend you give a read.

Stephen Walter 有一篇关于如何使用jQuery、ASP.NET 和浏览器历史记录的好文章它基本上描述了如何自己保存和恢复应用程序状态。我建议你读一读。

Elijah Manor(co-host of the Official jQuery Podcast) has also written a nice articleabout this topic.

Elijah Manor(官方 jQuery Podcast 的联合主持人)也写了一篇关于这个主题的好文章

I hope this helps -D

我希望这有帮助 -D

回答by Nishanthi Grashia

For JQM, this works!

对于 JQM,这有效!

$(document).ready(function(){
    $('.backLink').click(function(){
        parent.history.back();
        return false;
    });
});

回答by Xerrex

Try this: am using materialise css.

试试这个:我正在使用 materialize css。

<button id="back_btn" class="btn waves-effect waves-light"  name="action">Back<i class="material-icons left">chevron_left</i>
</button>

In your jquery script file add

在您的 jquery 脚本文件中添加

$("#back_btn").click(function (){
  window.history.back();
});

within the document ready function.

在文档准备功能中。

回答by coder

I think button onclick="history.back();"is one way to solve the problem.But it might not work in the following cases:

我认为button onclick="history.back();"是解决问题的一种方法。但在以下情况下可能不起作用:

  1. If the page gets refreshed or reloaded.
  2. If the user opens the link in a new page.
  1. 如果页面被刷新或重新加载。
  2. 如果用户在新页面中打开链接。

To overcome these, the following code could be used if you know which page you have to return to.

为了克服这些,如果您知道必须返回哪个页面,可以使用以下代码。

E.g. If you have a no of links on one page and the back button is to be used to return to that page.

例如,如果您在一个页面上没有链接,并且要使用后退按钮返回到该页面。

<input type="button" onclick="document.location.href='filename';" value="Back" name="button" class="btn">

<input type="button" onclick="document.location.href='filename';" value="Back" name="button" class="btn">