2

Is it possible to have both an "AJAX" hash mark # as well as a scroll down to one?

Thanks.

Taz
  • 3,718
  • 2
  • 37
  • 59
Francisc
  • 77,430
  • 63
  • 180
  • 276

2 Answers2

3

No.

The # is a special character in a URL, it marks the rest of the URL as a fragment identifier, so everything after it refers to an HTML element id, or a named anchor in the current page. (Source)

What's the shebang/hashbang (#!) in Facebook and new Twitter URLs for? - it's only for Googlebot.

Yes.

If you really want to have both, the scroll-to bit can be fudged width JavaScript. It wouldn't be pretty.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • I see, so basically something like #scrollToID/#!/data-for-ajax would do the trick? I always use hashbang for ajax related stuff anyway. – Francisc Mar 24 '11 at 22:16
  • 1
    Um, no. [This link (`#` only)](http://stackoverflow.com/questions/5426007/ajax-and-scroll-to/5426097#5426097) correctly scrolls to this answer, but [this link (`#` and `#!`)](http://stackoverflow.com/questions/5426007/ajax-and-scroll-to/5426097#5426097/#!data-for-ajax) doesn't, because the browser thinks you're trying to scroll to an element with ID `#5426097/#!data-for-ajax)`. Every page that you'd want this to work on would have to have some moderately gnarly JavaScript to make the page scroll to the correct element. – Matt Ball Mar 24 '11 at 22:25
  • 1
    The `#!` **only works with googlebot**. Browsers understand fragment identifiers (`#`) normally (hence the correct scroll behavior with no custom JavaScript needed). Browers don't understand `#!` any differently from `#` - browsers will interpret **everything** in a URL after the first `#` as the [fragment identifier](http://en.wikipedia.org/wiki/Fragment_identifier). – Matt Ball Mar 24 '11 at 22:30
1

As far as I know, # is not allowed in a fragment identifier. You could encode it as %23 and do something with that maybe.

If you're doing on-page stuff that you would like to be able to scroll to, you could just hijack links by adding a certain class to them or something.

Also, consider reading up on history.pushState and such on:

https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history

Matijs
  • 3,118
  • 2
  • 29
  • 41