<p>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. </p>

<p>How are people handling maintaining RESTfulness in AJAX apps? </p>

[{'answer_id': 1465, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<p>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.</p>\n\n<pre><code>var encodedState = base64(json(state));\nvar newLocation = oldLocationWithoutFragment + "#" + encodedState;\n\ndocument.location = newLocation; // adds new entry in browser history\ndocument.location.replace(newLocation); // replaces current entry in browser history\n</code></pre>\n\n<p>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.</p>\n'}, {'answer_id': 1468, 'author': 'Dave Ward', 'author_id': 60, 'author_profile': 'https://Stackoverflow.com/users/60', 'pm_score': 8, 'selected': True, 'text': '<p>The way to do this is to manipulate <code>location.hash</code> when AJAX updates result in a state change that you\'d like to have a discrete URL.  For example, if your page\'s url is:</p>\n\n<blockquote>\n  <p><a href="http://example.com/" rel="nofollow noreferrer">http://example.com/</a></p>\n</blockquote>\n\n<p>If a client side function executed this code:</p>\n\n<pre><code>// AJAX code to display the "foo" state goes here.\n\nlocation.hash = \'foo\';\n</code></pre>\n\n<p>Then, the URL displayed in the browser would be updated to:</p>\n\n<blockquote>\n  <p><a href="http://example.com/#foo" rel="nofollow noreferrer">http://example.com/#foo</a></p>\n</blockquote>\n\n<p>This allows users to bookmark the "foo" state of the page, and use the browser history to navigate between states.</p>\n\n<p>With this mechanism in place, you\'ll then need to parse out the hash portion of the URL on the client side using JavaScript to create and display the appropriate initial state, as fragment identifiers (the part after the #) are not sent to the server.</p>\n\n<p><a href="http://benalman.com/projects/jquery-hashchange-plugin/" rel="nofollow noreferrer">Ben Alman\'s hashchange plugin</a> makes the latter a breeze if you\'re using jQuery.</p>\n'}, {'answer_id': 37966, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>SWFAddress works in Flash &amp; Javascript projects and lets you create bookmarkable URLs (using the hash method mentioned above) as well as giving you back-button support.</p>\n\n<p><a href="http://www.asual.com/swfaddress/" rel="nofollow noreferrer">http://www.asual.com/swfaddress/</a></p>\n'}, {'answer_id': 700173, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>The window.location.hash method is the preferred way of doing things. For an explanation of how to do it, \n<a href="http://ajaxpatterns.org/Unique_URLs" rel="nofollow noreferrer">Ajax Patterns - Unique URLs</a>.</p>\n\n<p>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. <a href="http://developer.yahoo.com/yui/history/" rel="nofollow noreferrer">YUI Browser History Manager</a>.</p>\n\n<p>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.) </p>\n\n<p>IE needs an iframe based hack, where Firefox will produce double history using the same method.</p>\n'}, {'answer_id': 3928831, 'author': 'Marcelo', 'author_id': 475145, 'author_profile': 'https://Stackoverflow.com/users/475145', 'pm_score': 2, 'selected': False, 'text': "<p>Check if user is 'in' the page, when you click on the URL bar, JavaScript says you are out of page.\nIf 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 keyboard event command (document.onkeypress) from JavaScript will be able to check if it's enter and active the JavaScript for redirection.\nYou can check if user is IN the page with window.onfocus and check if he's out with window.onblur.</p>\n<p>Yeah, it's possible.</p>\n<p>;)</p>\n"}, {'answer_id': 4986440, 'author': 'daniel.wright', 'author_id': 439630, 'author_profile': 'https://Stackoverflow.com/users/439630', 'pm_score': 4, 'selected': False, 'text': "<p>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.</p>\n\n<p>Also github.com when looking at a navigating within a certain project.</p>\n"}, {'answer_id': 5801109, 'author': 'iesus', 'author_id': 726784, 'author_profile': 'https://Stackoverflow.com/users/726784', 'pm_score': 4, 'selected': False, 'text': '<p>It is unlikely the writer wants to reload or redirect his visitor when using Ajax.\nBut why not use HTML5\'s <code>pushState</code>/<code>replaceState</code>?</p>\n\n<p>You\'ll be able to modify the addressbar as much as you like. <a href="https://www.w3.org/TR/2010/WD-html5-20100624/history.html#history" rel="nofollow noreferrer">Get natural looking urls, with AJAX.</a></p>\n\n<p>Check out the code on my latest project: \n<a href="http://iesus.se/" rel="nofollow noreferrer">http://iesus.se/</a></p>\n'}, {'answer_id': 6451853, 'author': 'Neil', 'author_id': 181284, 'author_profile': 'https://Stackoverflow.com/users/181284', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n<p>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.</p>\n"}]
['https://Stackoverflow.com/questions/1457', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/331/']