Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  174] [ 4]  / answers: 1 / hits: 21081  / 13 Years ago, wed, january 25, 2012, 12:00:00

I have a page that dynamically loads content based on a user pushing a button:



${document).ready(function)
{
$(#myButton).click(function()
{
$(#dynamicDiv).load(www.example.com);
});
}


The dynamic content works fine, I can fetch pages all day long. But after you follow a link to another page, then press the browser back button to come back to the page, the page is completely reset as though no dynamic content had ever been loaded.



I swear I've seen different behavior before, but maybe I'm insane. Shouldn't the browser preserve the state of the page, rather than re-rendering it?



EDIT:
By the way, I'm using Play! framework, if that has any bearing on this.


More From » jquery

 Answers
14

The browser loads the page as it was first received. Any DOM modifications done via javascript will not be preserved.



If you want to preserve the modifications you will have to do some extra work. After you modify the DOM, update the url hash with an identifier that you can later parse and recreate the modification. Whenever the page is loaded you need to check for the presence of a hash and do the DOM modifications based on the identifier.



For example if you are displaying user information dynamically. Every time you display one you would change the url hash to something like this: #/user/john. Whenever the page loads you need to check if the hash exists (window.location.hash), parse it, and load the user information.


[#87802] Tuesday, January 24, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarettajb

Total Points: 678
Total Questions: 94
Total Answers: 90

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;