简体   繁体   中英

Input Type Button Go Back with History and Back to Top

I'm very new to JavaScript. I'm trying to make an HTML button to do 2 things (at the same time) when clicked:

1) Go back while retaining user inputs (there is a form in the previous page).

2) Go to the top of the previous page.

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).

2) Go to the top of the current page.

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.)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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