Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Michael Rockett
Michael Rockett
40,321 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,321 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,321 Points

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

Michael Rockett
Michael Rockett
40,321 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(); });