Skip to content

Instantly share code, notes, and snippets.

@sunny
Last active January 18, 2023 12:04
Show Gist options
  • Save sunny/481146 to your computer and use it in GitHub Desktop.
Save sunny/481146 to your computer and use it in GitHub Desktop.
// Change window.location.hash without scrolling the page, even in IE
// by renaming the element's id temporarily
function changeHashWithoutScrolling(hash) {
const id = hash.replace(/^.*#/, '')
const elem = document.getElementById(id)
elem.id = `${id}-tmp`
window.location.hash = hash
elem.id = id
}
@Peters8090
Copy link

You saved me ;)

@sunny
Copy link
Author

sunny commented Dec 23, 2020

Glad it could help! I've just updated the code to use ES6.

@andrii-marushchak
Copy link

change getElementBy to getElementById

@sunny
Copy link
Author

sunny commented Sep 12, 2021

Woops. Thanks for the heads-up, it’s now updated.

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