So I'm attempting to write a script that will load content dynamically so that the page doesn't ever have to be refreshed (the main content will be replaced, but the sidebar, header, footer, etc., will remain in the same place). I've gotten my event firing, and I'm using pushState()
to update my URL. However, I can't figure out how I should use the window.history.back()
and window.history.forward()
. Would I have to redefine those two functions in order to get it to successfully travel?

- 5,308
- 4
- 24
- 36
3 Answers
The easiest way I've found to do what (I think) you're after is to hook up your dynamically loading-ness to your window.location.hash, so:
window.location.hash='page_1';
You can then bind an event handler to the hash change and go AJAX down page 1.
Changing the hash causes a new history page, so you don't have to do anything with window.history at all.

- 46,179
- 22
- 132
- 191

- 3,312
- 1
- 24
- 31
You are using PushState
to update URL, so your history is already handled for you in all browsers that support that functionality.
But older browsers do not support PushState
, you might have to test
if (history.pushState) { //is supported }
I recommend that you use the History.js script here. It will revert to using the old onhashchange functionality, for HTML4 browsers and pushState for HTML5. You will not have to deal with history.forward
manually.
On a side-note, Also, you do need to configure your .htaccess, so that bookmarks and page-links of your website work correctly.

- 46,179
- 22
- 132
- 191
-
Well, the way I'm doing it is that each page is a full page within itself, and I'm pulling specific content out of there to place into the current page. So there won't be an issue if someone wants to bookmark this. As far as the compatibility issue, this is just a little project, and I'm working strictly for HTML5-compatible browsers. I guess what I really want to do is to bind a specific action to the window.history.back() function. – Chad Jul 21 '12 at 08:50
-
Okay. Why would you want to bind an action to that function? `window.history.back`, if called from within javascript, will take the browser one step back. If the user presses the backbutton and you want to detect it, you could use [this](http://benalman.com/projects/jquery-bbq-plugin/). See similar question at http://stackoverflow.com/questions/172957/detecting-back-button-hash-change-in-url – Anirudh Ramanathan Jul 21 '12 at 08:56
Though pushState()
is history, you need to pass the URL so that the page will load without refreshing it.But windows.history.back()
will take you the last page accessed from browser cache automatically.Here you not passing any URL.And the same is applicable to windows.history.forward()
as well.

- 9,776
- 6
- 41
- 66
-
-
By default, browser cache page.So next time if the user tries to invoke the same page, browser will fetch it from the cache unless it is MODIFIED.In this case, if you call windows.history.back(), it will take last visited page from CACHE. – UVM Jul 21 '12 at 08:23
-
Right, but my question is how do I get around the problem if I am modifying these pages? Can I force a refresh on the back button? – Chad Jul 21 '12 at 08:27
-
You can.Please check this link.http://www.webdeveloper.com/forum/showthread.php?t=137518 – UVM Jul 21 '12 at 08:31