-5

Similar to SO, I want to have an anchor for some comment and after the page is loaded I want the page to do some javascript (highlight,scrolling, etc - that I can write myself)

So If my url is

www.mywebapp.com/mypost#45625

I'd like the javascript to scroll to div with id 45625 and do my own javascript there.

How can I write the condition for which to start the execution of my javascript so it would work only if an achor exists?

3
  • 1
    This is done automatically. Try it out. Commented Mar 17, 2013 at 4:58
  • @Blender, "and do some stuff there." Commented Mar 17, 2013 at 4:59
  • 1
    Use id="45625" rather than id="#45625". Commented Mar 17, 2013 at 5:00

1 Answer 1

8
if (window.location.href.indexOf('#') > -1)
    // Do something.

Or even better:

if (window.location.hash.length)
    ...
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.