I have some jQuery/JavaScript code that I want to run only when there is a hash (#) anchor link in a URL. How can you check for this character using JavaScript?
#
You can parse urls using modern JS:
var my_url = new URL('http://www.google.sk/foo?boo=123#baz'); my_url.hash; // outputs "#baz" my_url.pathname; // outputs "/moo" my_url.protocol; // "http:" my_url.search; // outputs "?doo=123"
urls with no hash will return empty string.