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?
#
If the URI is not the document's location this snippet will do what you want.
var url = 'example.com/page.html#anchor', hash = url.split('#')[1]; if (hash) { alert(hash) } else { // do something else }