Skip to content

Instantly share code, notes, and snippets.

@w33tmaricich
Last active November 16, 2021 06:38
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save w33tmaricich/7009931 to your computer and use it in GitHub Desktop.
Save w33tmaricich/7009931 to your computer and use it in GitHub Desktop.
window.onload = function () {
if (typeof history.pushState === "function") {
history.pushState("jibberish", null, null);
window.onpopstate = function () {
history.pushState('newjibberish', null, null);
// Handle the back (or forward) buttons here
// Will NOT handle refresh, use onbeforeunload for this.
};
}
else {
var ignoreHashChange = true;
window.onhashchange = function () {
if (!ignoreHashChange) {
ignoreHashChange = true;
window.location.hash = Math.random();
// Detect and redirect change here
// Works in older FF and IE9
// * it does mess with your hash symbol (anchor?) pound sign
// delimiter on the end of the URL
}
else {
ignoreHashChange = false;
}
};
}
}
@aapiro
Copy link

aapiro commented Jan 22, 2016

Good job dude! you save me!

@jenstornell
Copy link

That worked but I don't know what the else is for. For me it seems to be enough with the if part.

@Harshi11
Copy link

Can anyone explain me please ..why we are pushing null before popstate and again we are pushing null

@deepak29verma
Copy link

what is jibberish" and 'newjibberish'

@Royalone94
Copy link

I think it is mock data

@jackbaron
Copy link

it does not working browser chrome on ios

@peterPP1
Copy link

so it seems that the code doesn't always work in Chrome, sometimes it works sometimes it doesn't. Anyone has an idea on how to adjust the code to always work in Chrome?

@sachinparadkar
Copy link

This doesn't seem to work in Chrome , anyone has an idea on how to make this work on Chrome ?

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