Starlin
Starlin

Reputation: 845

javascript detect # in url

My application is an iframe app so when the user changes page they do not automatically get taken to the top. To combat this on page load I call

window.location.hash = 'tophash'

I have found however on some rare cases I need to take the user to a specific part of the page. So I make a url with #anotherID on the end. Problem is currently they are taken to tophash on page load.

What I need is if there is a hash within the url it does not run window.location.hash = 'tophash'

So my question is... how do I detech the presence of a # in the url?

Upvotes: 6

Views: 764

Answers (1)

Pekka
Pekka

Reputation: 449465

Querying the hash property before setting it should do the job.

if ((!window.location.hash) || (window.location.hash == "#"))
 window.location.hash = "tophash";

Upvotes: 3

Related Questions