Organizational Research By

Surprising Reserch Topic

How can you check for a #hash in a URL using JavaScript?


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? I need a simple catch-all test that would detect URLs like these:

  • example.com/page.html#anchor
  • example.com/page.html#anotheranchor

Basically something along the lines of:

if (thereIsAHashInTheUrl) {        
    do this;
} else {
    do this;
}

If anyone could point me in the right direction, that would be much appreciated.


asked May 20, 2015 in JAVASCRIPT by rahulgii
0 votes
78 views



Related Hot Questions

7 Answers

0 votes
NULL
answered May 20, 2015 by rahulgii
0 votes
NULL
answered May 20, 2015 by rahulgii
0 votes
NULL
answered May 20, 2015 by rahulgii
0 votes
NULL
answered May 20, 2015 by rahulgii
0 votes
NULL
answered May 20, 2015 by rahulgii
0 votes
NULL
answered May 20, 2015 by rahulgii
0 votes
NULL
answered May 20, 2015 by rahulgii

...