admin管理员组文章数量:1315792
I'm using jQuery's appendTo() method to append items to an unordered list. When the user clicks another link, and then presses the Back button, the appended items disappear. Is there a way to persist those changes using JavaScript?
I'm using jQuery's appendTo() method to append items to an unordered list. When the user clicks another link, and then presses the Back button, the appended items disappear. Is there a way to persist those changes using JavaScript?
Share Improve this question asked Aug 14, 2011 at 17:58 ustunustun 7,0415 gold badges46 silver badges57 bronze badges3 Answers
Reset to default 4When hitting BACK, some browsers cache te previous (originally loaded) page. If you can regenerate the page with a fresh reload you can use cache-control 'no-store, no-cache, must-revalidate' to force this on a BACK.
Chrome wants no-store, and IE wants must-revalidate. For other browsers (and w3c) no-cache is enough.
Once your user navigates away from your page, the DOM is discarded. When your user hits the back button, they get a fresh copy of the HTML from your server (or from their cache).
So, you have one of 2 options:
The correct way would be to save the DOM state in a cookie (or session), and then on page load check if that cookie is present, and if so, append that info.
Since you have not provided enough information, I'm not exactly sure what you're trying to acplish. So, if a cookie is not good enough, you might have to attach a click event to the link, and instead of just sending them off to that link, you'll first store the DOM in a variable
var theDOM = $('html').clone(true);
, and then load in the HTML for that link with an AJAX request.
You'll also have to inform the browser that your history state has changed. That can be acplished with HTML5's new History API. In older browsers you can do something similar via the hash part of the URL (although in older versions of IE even that won't work). Then, when the user clicks the back button, you'll reload that DOM from your variable...
Either way, this is WAY too plicated. You're probably looking at this the wrong way. What are you trying to acplish?
If you dont have any problem to use iframe
then you can use it to preserve the previous data. On iframe
load event write a js code which will take the preserved data within it and append it to desired element. This way when you navigate to next page and press back button the iframe will load and then the load event handler will do its job,
版权声明:本文标题:javascript - Persisting DOM changes after clicking a link and then pressing the Back button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741990687a2409046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论