Skip to main content

Modify Address Bar URL in AJAX App to Match Current State


I'm writing an AJAX app, but as the user moves through the app, I'd like the URL in the address bar to update despite the lack of page reloads. Basically, I'd like for them to be able to bookmark at any point and thereby return to the current state.



How are people handling maintaining RESTfulness in AJAX apps? Thanks in advance.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. Look at sites like book.cakephp.org. This site changes the URL without using the hash and use AJAX. I'm not sure how it does it exactly but I've been trying to figure it out. If anyone knows, let me know.

    Also github.com when looking at a navigating within a certain project.

    ReplyDelete
  2. This is similar to what Kevin said. You can have your client state as some javascript object, and when you want to save the state, you serialize the object (using JSON and base64 encoding). You can then set the fragment of the href to this string.

    var encodedState = base64(json(state));var newLocation = oldLocationWithoutFragment + "#" + encodedState;document.location = newLocation; // adds new entry in browser historydocument.location.replace(newLocation); // replaces current entry in browser history

    The first way will treat the new state as a new location (so the back button will take them to the previous location). The latter does not.

    ReplyDelete
  3. It is unlikely the writer wants to reload or redirect his visitor when using Ajax.
    But why not use HTML5's pushState/replaceState?

    You'll be able to modify the addressbar as much as you like. Get natural looking urls, with ajax: http://www.w3.org/TR/html5/history.html#the-history-interface

    Check out the code on my latest project:
    http://iesus.se/

    ReplyDelete
  4. Basically your links contain anchors:


    page.html#Tab1
    page.html#Tab2
    page.html#Tab3


    And when the page loads you check document.location.href and substr the ID off the end to get the state. At least that's how I've been doing it. :(

    Hopefully there's a better way.

    ReplyDelete
  5. SWFAddress works in Flash & Javascript projects and lets you create bookmarkable URLs (using the hash method mentioned above) as well as giving you back-button support.

    http://www.asual.com/swfaddress/

    ReplyDelete
  6. The window.location.hash method is the preferred way of doing things. For an explanation of how to do it,
    Ajax Patterns - Unique URLs.

    YUI has an implementation of this pattern as a module, which includes IE specific work arounds for getting the back button working along with re-writing the address using the hash. YUI Browser History Manager.

    Other frameworks have similar implementations as well. The important point is if you want the history to work along with the re-writing the address, the different browsers need different ways of handling it. (This is detailed in the first link article.)

    IE needs an iframe based hack, where Firefox will produce double history using the same method.

    ReplyDelete
  7. Check if user is 'in' the page, when you click on the url bar, javascript says you are out of page.
    If you change the url bar and press 'ENTER' with the symbol '#' within it then you go into the page again, without click on the page manually with mouse cursor, then a keyboad event command (document.onkeypress) from javascript will be able to check if it's enter and active the javascript for redirection.
    You can check if user is IN the page with window.onfocus and check if he's out with window.onblur.

    Yeah, it's possible.

    ;)

    ReplyDelete
  8. If OP or others are still looking for a way to do modify browser history to enable state, using pushState and replaceState, as suggested by IESUS, is the 'right' way to do it now. It's main advantage over location.hash seems to be that it creates actual urls, not just hashes. If browser history using hashes is saved, and then revisited with javascript disabled, the app won't work, since the hashes aren't sent to the server. However, if pushState has been used, the entire route will be sent to the server, which you can then build to respond appropriately to the routes. I saw an example where the same mustache templates were used on both the server and the client side. If the client had javascript enabled, he would get snappy responses by avoiding the roundtrip to the server, but the app would work perfectly fine without the javascript. Thus, the app can gracefully degrade in the absence of javascript.

    Also, I believe there is some framework out there, with a name like history.js. For browsers that support HTML5, it uses pushState, but if the browser doesn't support that, it automatically falls back to using hashes.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.