4757 comments
2071 subscribers
4480 on Twitter
Subscribe! Feed reader E-mail

Drupal in the trenches: AJAX history makes my brain hurt

Many websites use asynchronous Javascript and XML (AJAX) to provide all sorts of whizbang improvements, such as smooth interaction without page reloads. It took me a week to figure out how to do all the effects specified in our information architecture document: callouts, modal dialogs, in-page calendar navigation, and so on. I was pretty happy with what I did, considering it was my first serious work with JQuery.

Then my project manager said, “If I go to the event details page from the month view and hit back, it takes me to the day view instead.”

I said, “Welcome to the wonderful world of AJAX. This might be tough to fix.”

Making the back button work in AJAX applications requires a lot of effort. It doesn’t look like people have a nice and clean solution for it yet, although there are a number of libraries that try to address the situation.

Following the principle of progressive enhancement, I had built all the plain HTML functionality first, then layered the Javascript on top of it using jQuery callbacks. In addition to ensuring that the site still works even if Javascript is disabled, this approach also helps make sure that I have proper URLs for almost all the webpages involved. (I didn’t bother with explicitly transient pages like the year navigator or the day pop-up.)

I started with this Hijax-based approach, because it had the most documentation. I had problems getting it to behave, though, because my AJAX pages have other AJAX links that fail with the history-remote plugin. The history_remote plugin works by replacing all the links with the current page and a code (#remote-1, for example). When the back button is pressed, the library looks for the appropriate link and triggers the click event. This breaks down when the link isn’t actually on the first page. For example, when a user switches from a week to a month view, then goes to the next month, the plugin can’t find the link to the next month on the week view’s page, which is where the user started.

What I really needed to do is encode more information in the URL. If I encode information in the anchor portion of the URL (#…), I can use that to request the page and put that into the appropriate div. For example, if I pass in #transitions_content?new_path=connect/calendar/2009/05 , I might be able to parse that and put the content of new_path into the transitions_content div.

I started going down that rabbit-hole, and then I got myself thoroughly confused, so I decided that the best way would be to just rip out the major AJAX navigation and go back to the simple stuff (which fortunately still works).

Gah. Brain hurts!

Does anyone have a clean way to do this?

Short URL: http://sachachua.com/blog/p/6298

6 Responses to “Drupal in the trenches: AJAX history makes my brain hurt”

  1. You could rig Hijax to remember and replicate a sequence of links. But that would get ugly quickly, particularly when it remembers lots of clicks that aren’t actually necessary in order to get back to where the user wants to be. Although you could just make that a feature.. instant replay!

    The best way to me seems just to do your own parsing of the anchor portion of the URL and figure out how to interpret it. You already have the information available (somewhere you’ve written code to load the data you need to reload). So it’s just a matter of implementing a bit of Javascript to prepare the appropriate data on page load.

    A good way to do this easily would perhaps be to have a well-defined mapping between the names of server-side resources from which you’re loading data and the hashtags you’re saving. So then the process becomes: read hashtag, transform into URL, load data from URL and populate.

    To be fair, I haven’t actually done this before, and I may just be repeating what you said you’ve tried and didn’t work, but it sounds reasonable to me…..

  2. I’have same problem on my project made with symfony. I want to do the same uri like facebook or other site made with ajax. Parse uri and load content on my object.
    I’m trying to do this with my hand :)
    PS : sorry for my english

  3. http://www.seaside.st handles all that, but it lacks Drupal or an equivalent and is smalltalk. But you can AJAX everywhere and the back button keeps up.

  4. How’s this different? Well first, we make use of the jQuery.post function rather than using the lower level jQuery.ajax with POST specified as an argument. There is an equally useful jQuery.get option if we wanted to use a GET request instead. Note the form:first selector, obviously that would only grab the data for the first form on the page but since there is only one form in the example HTML we’re ok.

  5. ———————————————————————————————————————–
    Use AddHistoryPoint() method of ScriptManger by enabling history paramer. And then implement onNavigate event, which will fire on Back Buttons event. And inside OnNavigate event, get state of ur previous Ajaxified page using HistoryEventArgs.

    For details try googling this words and you will find better articles then what I wrote.

  6. I realise this was ages ago, but the jQuery history plugin was just what you needed. It handles the browser history using the anchor portion of the URL, and all you have to do is provide a function that takes the anchor portion as an argument and loads the relevant AJAX state (although the documentation is so sparse it took me a long time to figure that out).

    http://www.mikage.to/jquery/jquery_history.html

Comment, share a thought, ask a question...

Please comment as you, not your organization.





On This Day...

  • 2011: The flow of opportunities in a large company — Henry Will asked me how I got to work on such interesting projects. What worked particularly well: blogs? presentations? networking? [...]
  • 2010: Exponential awesomeness —   @smeech I recently built an entire workshop around Sacha Chua‘s Teacher’s Guide to Web 2.0: http://ow.ly/160X0 Watch/Do/Teach was our mantra @sachac [...]
  • 2008: New blog design — Well, it’s still really the Networker-10 theme underneath, but I’ve stripped away a lot of the CSS that made my [...]
  • 2008: Thinking out loud: Taking it off/online — | View | Upload your own Do you want to get more people you know to read your blog, connect [...]
  • 2008: Book: Success Built to Last — In Success Built to Last (Jerry Porras, Stewart Emery, and Mark Thompson), you’ll learn that passion is essential to lasting [...]
  • 2008: Weekly review: Week ending May 25, 2008 — This week was mostly about working on Drupal code. =) Finding my groove! I’ve also been working on my book. It’s [...]
  • 2008: Getting through the plateau of mediocrity; Picking up the idioms — This week, I discovered–rediscovered?–something I love about programming and broke through something that had been frustrating me for a while. [...]
  • 2007: Pinball — I’ve always been fascinated by the flashing lights and mechanical contraptions of pinball machines, so it was with great delight that [...]
  • 2007: Driving me crazy — The acrid smell of burnt rubber filled the driveway of our Alabang house, testimony to the difficulties I was having with [...]
  • 2007: Exercise — We sometimes do high-five pushups during krav maga exercise/self-defense classes. I haven’t gone in a while, but feeling an itch to do [...]
  • 2004: Open source CMS demos — opensourceCMS (http://www.opensourcecms.com/) has demos of content management systems. (Recent defacement: uncool. Bad karma for whoever did it.)
  • 2004: “Europe’s Semantic Web Projects Start to Mesh” — The inaugural European Semantic Web Symposium held in Crete brought together international experts and showcased how semantic Web technology has moved from [...]