Skip to content

Instantly share code, notes, and snippets.

@asmblah
Last active January 2, 2016 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asmblah/8349213 to your computer and use it in GitHub Desktop.
Save asmblah/8349213 to your computer and use it in GitHub Desktop.
Fixes iOS7.0.4's standalone webapp bug
(function fixIOS7HistoryState(global) {
// Ensure we are running as a standalone webapp, in a version supporting the History.state property
if (navigator.standalone && typeof browserHistory.state !== "undefined") {
var browserLocation = global.location,
fakeState = {},
key = Math.random(),
location = browserLocation.href,
oldState = browserHistory.state,
title = global.document.title;
// Push a “fake” state object with a random key to avoid false positives with existing state objects
fakeState[key] = true;
browserHistory.replaceState(fakeState, title, location);
function restoreOldState() {
browserHistory.replaceState(oldState, title, location);
}
if (!browserHistory.state || !browserHistory.state[key]) {
// History stack is not working, apply fix
restoreOldState();
// Force browser to create a new history stack by navigating to a different protocol than the current one, then return
browserLocation.href = "data:text/html;base64," + global.btoa("<script>location.href = " + JSON.stringify(location) + ";</script>");
}
// Restore the correct history state, overwriting our “fake” one used to test
restoreOldState();
}
}(window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment