Skip to content

Instantly share code, notes, and snippets.

@atomita
Last active January 9, 2019 01:29
Show Gist options
  • Save atomita/afaf21ba2513cdc4e22ed6af94252db8 to your computer and use it in GitHub Desktop.
Save atomita/afaf21ba2513cdc4e22ed6af94252db8 to your computer and use it in GitHub Desktop.
const prevPage = document.referrer
if (origin(createAnchor(prevPage)) === origin(window.location)) {
window.history.pushState(location.href, '', '/')
} else {
window.history.back()
}
function createAnchor(url) {
const anchor = document.createElement('a')
anchor.href = url
return anchor
}
function origin(loc) {
var protocol = (loc.protocol && ':' !== loc.protocol) ? loc.protocol : location.protocol
var host = [loc.hostname, parseInt(loc.port, 10) === 80 ? 0 : loc.port].filter(Boolean).join(":")
|| [location.hostname, parseInt(location.port, 10) === 80 ? 0 : location.port].filter(Boolean).join(":")
return protocol + "//" + host
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment