How to detect the #hash and get the hash in JavaScript?

How to detect the #hash and get the hash in JavaScript? It’s better if no additional library is needed. That said, jquery is fine if it is needed.

You can use this piece of JavaScript code:

if (window.location.hash) {
    // hash found
    var hash = window.location.hash.substring(1);
} else {
    // No hash
}
Answered by Eric Z Ma.

Eric Z Ma

Eric is a father and systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *