简体   繁体   English

输入类型按钮返回历史记录并返回顶部

[英]Input Type Button Go Back with History and Back to Top

I'm very new to JavaScript. 我对JavaScript非常陌生。 I'm trying to make an HTML button to do 2 things (at the same time) when clicked: 我试图使HTML按钮在单击时可以同时执行2件事:

1) Go back while retaining user inputs (there is a form in the previous page). 1)返回,同时保留用户输入(上一页中有一个表格)。

2) Go to the top of the previous page. 2)转到上一页的顶部。

Here is what I have: 这是我所拥有的:

<input type="button" value="Go Back" onclick="javascript:history.go(-1); location.href='#'" />

UPDATE: 更新:

<script type="text/javascript">
function goBack() {
    window.history.go(-1);
    window.location.href = "#";
}
</script>

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

The button does not do anything when clicked though. 但是,单击该按钮不会执行任何操作。

This is what the code does: 这是代码的作用:

1) Go back while retaining user inputs (there is a form in the previous page). 1)返回,同时保留用户输入(上一页中有一个表格)。

2) Go to the top of the current page. 2)转到当前页面的顶部。

So, the first action is cancelled by the second action. 因此,第一个动作被第二个动作取消。

What you would need is to do the second step in the target page, ie code on the previous page that goes to the top when the page loads. 您需要做的是在目标页面中执行第二步,即上一页中的代码在页面加载时到达顶部。

In the current page you would have just: 在当前页面中,您将只有:

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

In the previous page you would have a script: 在上一页中,您将具有一个脚本:

window.onload = function(){
  window.scrollTo(0, 0);
};

(The script would naturally also run when the page initially loads (before it's the "previous page"), but that's not a problem because then the page is already at the top.) (该脚本自然也将在页面首次加载时运行(在它成为“上一页”之前),但这不是问题,因为那时页面已经在顶部了。)

另外,我只是在上一页上放置了一个“返回顶部”按钮,以确保它在所有浏览器上都可以使用。

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

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