I need to work with an ajax powered web page so I can pass an id or ids in the URL and it’ll only display result(s) based on the id(ids) passed in. For an old cgi page, I could do: http://myurl?id=1,
is there a way of doing this for Ajax pages?
Thanks,
David
Simplest – use anchors and the
hashchangeevent to modify the state of the URL (you’ll mostly need a shiv for older browsers like IE 6 and 7). When the page first loads, parse the hash and load what you need to via ajax. (An example of library to help with that Ben Alman’s jQuerybbqplugin.) Your URLs in this case will look like this:Neater – use the new
historyAPI topushStateas the page changes for newer browsers and fallback onhashchangefor browsers that don’t support the new HTML5 goodness. (An example of a library that helps with that is Ben Lupton’s History.js plugin). Your URLs for newer browsers will look like this:while URLs for browsers that don’t support the new API will see the
hashchangeURL.Neatest – use a full routing or MVC framework and don’t re-invent the wheel. Examples include, but are not limited to:
Note that with the newer
historyAPI, you really should make sure that all of your URLs are actually reachable and usable without JavaScript. (So if you pushhttp://example.com/my/app?q=1onto the history stack you should be able to type that URL into your browser’s address bar and just go there.)