How to "bookmark" page or content fetched using AJAX?
Asked Answered
S

5

6

How to "bookmark" page or content fetched using AJAX?

It looks like it can be easy if we just add the details to the "anchor", and then, use the routing or even in PHP code or Ruby on Rails's route.rb, to catch that part, and then show the content or page accordingly? (show the whole page or partial content)

Then it can be very simple? It looks like that's how facebook does it. What are other good ways to do it?

Shenitashenk answered 26/6, 2010 at 8:37 Comment(0)
S
0

I tried many packages. The jQuery History plugin seems to be most complete:

http://github.com/tkyk/jquery-history-plugin

Shenitashenk answered 20/8, 2010 at 16:15 Comment(0)
S
7

Update: There is now the HTML5 History API (pushState, popState) which deprecates the HTML4 hashchange functionality. History.js provides cross-browser compatibility and an optional hashchange fallback for HTML4 browsers.

To store the history of a page, the most popular and full featured/supported way is using hashchanges. This means that say you go from yoursite/page.html#page1 to yoursite/page.html#page2 you can track that change, and because we are using hashes it can be picked up by bookmarks and back and forward buttons.

You can find a great way to bind to hash changes using the jQuery History project http://www.balupton.com/projects/jquery-history

There is also a full featured AJAX extension for it, allowing you to easily integrate Ajax requests to your states/hashes to transform your website into a full featured Web 2.0 Application: http://www.balupton.com/projects/jquery-ajaxy

They both provide great documentation on their demo pages to explain what is happening and what is going on.

Here is an example of using jQuery History (as taken from the demo site):

// Bind a handler for ALL hash/state changes
$.History.bind(function(state){
    // Update the current element to indicate which state we are now on
    $current.text('Our current state is: ['+state+']');
    // Update the page"s title with our current state on the end
    document.title = document_title + ' | ' + state;
});

// Bind a handler for state: apricots
$.History.bind('/apricots',function(state){
    // Update Menu
    updateMenu(state);
    // Show apricots tab, hide the other tabs
    $tabs.hide();
    $apricots.stop(true,true).fadeIn(200);
});

And an example of jQuery Ajaxy (as taken from the demo site):

        'page': {
            selector: '.ajaxy-page',
            matches: /^\/pages\/?/,
            request: function(){
                // Log what is happening
                window.console.debug('$.Ajaxy.configure.Controllers.page.request', [this,arguments]);
                // Adjust Menu
                $menu.children('.active').removeClass('active');
                // Hide Content
                $content.stop(true,true).fadeOut(400);
                // Return true
                return true;
            },
            response: function(){
                // Prepare
                var Ajaxy = $.Ajaxy; var data = this.State.Response.data; var state = this.state;
                // Log what is happening
                window.console.debug('$.Ajaxy.configure.Controllers.page.response', [this,arguments], data, state);
                // Adjust Menu
                $menu.children(':has(a[href*="'+state+'"])').addClass('active').siblings('.active').removeClass('active');
                // Show Content
                var Action = this;
                $content.html(data.content).fadeIn(400,function(){
                    Action.documentReady($content);
                });
                // Return true
                return true;

And if you ever want to get the querystring params (so yoursite/page.html#page1?a.b=1&a.c=2) you can just use:

$.History.bind(function(state){
    var params = state.queryStringToJSON(); // would give you back {a:{b:1,c:2}}
}

So check out those demo links to see them in action, and for all installation and usage details.

Shrub answered 3/8, 2010 at 9:20 Comment(0)
U
1

If you use jquery, you can do that in a simple manner. just use ajaxify plugin. it can manage bookmarking of ajax pages and many other things.

Unicellular answered 26/6, 2010 at 8:42 Comment(0)
F
1

Check this, something may help you:

  1. How to change URL from javascript: http://doet.habrahabr.ru/blog/15736/
  2. How to pack the app state into url: http://habrahabr.ru/blogs/javascript/92505/
  3. An approach description: http://habrahabr.ru/blogs/webstandards/92300/

Note: all articles are in Russian, so either Google Translate them, or just review the code and guess the details.

Fettling answered 3/8, 2010 at 9:42 Comment(0)
P
1

Take a look to the Single Page Interface Manifesto

Plio answered 9/2, 2011 at 16:27 Comment(0)
S
0

I tried many packages. The jQuery History plugin seems to be most complete:

http://github.com/tkyk/jquery-history-plugin

Shenitashenk answered 20/8, 2010 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.