2

I'm trying to load my content into a div using Ajax. So far I've been successful into getting it to load. However, I don't know how to handle when...

  1. The user inputs the url (http://www.example.com/#page.html) is should direct them to the page with the correct content loaded. (Right now it just loads it normally.)
  2. When the user hits the BACK button on their browser it should navigate properly.

Any suggestions and concerns are welcome also. Thanks for your assistance.

Nickolay
  • 31,095
  • 13
  • 107
  • 185
Ber53rker
  • 669
  • 3
  • 10
  • 21

1 Answers1

0

case A

Upon loading the page, let JavaScript check whether or not there is a hash present and act accordingly. Load content based on hash if there is a hash, otherwise show default page. ( be sure to validate the hash! )

var hash = window.location.hash /* get the hash from the url */

case B

Assuming you're constantly browsing 'from hash to hash', the browser should already do this correctly. Requirement is that you implement case A as described above.


Example:

  1. You visit example.com
  2. You click through to example.com/#MyAwesomeAjaxLoadedContent
  3. You click through to example.com/#MoreAwesomeAjaxContent
  4. You click the back button, you're now at example.com/#MyAwesomeAjaxLoadedContent

If you implemented case A correctly, your browser should now display example.com/#MyAwesomeAjaxLoadedContent

Willem
  • 723
  • 2
  • 9
  • 27
  • But if I'm using Ajax and not causing postbacks then I can't do it on pageload right? – Ber53rker Nov 18 '11 at 13:18
  • Check the hash on pageload, for cases that occur after the initial pageload, take a look at this question: http://stackoverflow.com/questions/680785/on-window-location-hash-change – Willem Nov 18 '11 at 13:30