Skip to content

Instantly share code, notes, and snippets.

@thomseddon
Created October 4, 2012 16:21
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save thomseddon/3834721 to your computer and use it in GitHub Desktop.
Save thomseddon/3834721 to your computer and use it in GitHub Desktop.
AngularJS: Avoid redirection loop in legacy browser (IE) with HTML5 mode
var buggyAndroid = parseInt((/android (\d+)/.exec(window.navigator.userAgent.toLowerCase()) || [])[1], 10) < 4;
if (!history.pushState || buggyAndroid) {
if (window.location.hash) {
if(window.location.pathname !== '/') window.location.replace('/#!' + window.location.hash.substr(2)); //Hash and a path, just keep the hash (redirect)
} else {
window.location.replace('/#!' + window.location.pathname); //No hash, take path
}
}
//And then in app.config:
//$locationProvider.html5Mode(true).hashPrefix('!');
@ryangasparini-wf
Copy link

Do you have to rewrite all your route HREFs to hashes after this fix or does Angular know how to convert them into hashes?

@thomseddon
Copy link
Author

@ryangasparini-wf Angular doesn't convert them, but it does properly switch between pushstate and hashes if you call $location.path(...)

@sterlingwes
Copy link

Thank you for sharing this! :)

@voltechs
Copy link

Where does the initial if statement go?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment