<p>I have some jQuery/JavaScript code that I want to run only when there is a hash (<code>#</code>) anchor link in a URL. How can you check for this character using JavaScript? I need a simple catch-all test that would detect URLs like these:</p>
<ul>
<li><code>example.com/page.html#anchor</code></li>
<li><code>example.com/page.html#anotheranchor</code></li>
</ul>
<p>Basically something along the lines of:</p>
<pre><code>if (thereIsAHashInTheUrl) {
    do this;
} else {
    do this;
}
</code></pre>

Simple use of
location hash
:
```
```
if(window.location.hash) {
  // Fragment exists
} else {
  // Fragment doesn't exist
}
```
```