Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While BBQ was nice, it did not entirely solve my problem. For it to work in Chrome / Safari, you need to update the hash before the onbeforeunload event. Part of the requirement however made it so that was the only real time that I could save the state of the page. </p> <p>I came up with a solution, using both of the other answers here, using pushState and BBQ. I thought I would post it incase anyone else has the same issue I did.</p> <pre><code>function BackButtonState(saveStateCallback, pageLoadCallback) { var executePopStateCounter = null; var myData = {}; var browserCanPushState = history.pushState ? true : false; window.onbeforeunload = saveStateCallback; if (browserCanPushState) { //The version of safari this was tested on (5.0.3) uses an outdated version of //Webkit that has a bug where popstate is not called on the first page load. //This is a hacky work around until the problem is fixed. var agt = navigator.userAgent.toLowerCase(); if (agt.indexOf("safari") != -1) { executePopStateCounter = setTimeout(pageLoadCallback, 500); }; window.onpopstate = function(popEvent) { clearTimeout(executePopStateCounter); if (popEvent.state != null) { myData = popEvent.state; } pageLoadCallback(); } } else { $(document).ready(pageLoadCallback); } this.savePageState = function(state) { if (browserCanPushState) { history.pushState(state, 'BackButtonState'); } else { $.bbq.pushState(state); } } this.getState = function(item) { if (browserCanPushState) { return myData[item]; } else { return $.bbq.getState(item); } } this.deserializeSortList = function(sortArrays) { var sortList = []; $.each(sortArrays, function(index, pair) { sortList.push([parseInt(pair[0]), parseInt(pair[1])]); }); return sortList; } } </code></pre> <p>To use this, you do something like:</p> <pre><code>function saveState() { var myData = { bananas: 'are tasty' }; _backButton.saveState(myData); } function pageSetup() { //Do normal $(document).ready() stuff here var myOpinionOnFruit = _backButton.getState('bananas'); } var _backButton = new BackButtonState(saveState, pageSetup); </code></pre> <p>This is not guarenteed to be bug free, and I am unhappy with the hack I had to do for Safari. I just wanted to post this before I forgot.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload