Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Michael Rockett
Michael Rockett
40,365 Points

How can I use make the browser back button refresh the page?

Hi,

I'm trying to use javascript/jquery to cause the browser back button to refresh the previous page or redirect to another page. This is because I have a form that I want to be cleared when a user goes back.

Thanks, Mike

Jacobus Hindson
Jacobus Hindson
14,429 Points
<body onload="document.refresh();">  

This would enforce the page to reload anytime it is hit. Give it a try.

Michael Rockett
Michael Rockett
40,365 Points

Hi, thanks for your answer but it still doesn't seem to do it for some reason.

2 Answers

Hi Michael,

You might want to take a look at this discussion on StackOverflow

I've spent some time this morning researching this question and it looks like it's more complicated than it appears, so the above link may not be exactly what you need, but hopefully will help you get on the right track to a solution.

Michael Rockett
Michael Rockett
40,365 Points

Thanks, I haven't solved it yet but I think this has put me in the right direction

Michael Rockett
Michael Rockett
40,365 Points

I have found a solution. I've got the page to clear all the form fields when the back button is pressed. The code is below for anyone who has the same problem.

$(window).bind("pageshow", function() { // update hidden input field $('#formid')[0].reset(); });