Last active
January 2, 2016 19:19
-
-
Save asmblah/8349213 to your computer and use it in GitHub Desktop.
Fixes iOS7.0.4's standalone webapp bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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