如何“添加书签”使用 AJAX 获取页面或内容?

发布于 2024-09-07 07:08:06 字数 205 浏览 7 评论 0 原文

如何为使用 AJAX 获取的页面或内容添加“书签”?

看起来,如果我们只是将详细信息添加到“锚点”,然后使用路由,甚至在 PHP 代码或 Ruby on Rails 的route.rb 中,捕获该部分,然后显示内容或页面,这似乎很容易因此? (显示整个页面或部分内容)

那么可以很简单吗?看起来 Facebook 就是这么做的。还有哪些其他好的方法可以做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

滴情不沾 2024-09-14 07:08:06

更新:现在有 HTML5 History API(pushState、popState),它弃用了 HTML4 hashchange 功能。 History.js 提供跨浏览器兼容性和 可选 hashchange HTML4 浏览器的后备。

存储历史记录页面上,最流行且功能齐全/支持的方式是使用 hashchanges。这意味着,假设您从 yoursite/page.html#page1 转到 yoursite/page.html#page2,您可以跟踪该更改,并且因为我们使用哈希值,所以可以跟踪该更改可以通过书签以及后退和前进按钮来拾取。

您可以使用 jQuery History 项目找到一种绑定哈希更改的好方法
http://www.balupton.com/projects/jquery-history

还有一个全功能的 AJAX 扩展,允许您轻松地将 Ajax 请求集成到您的状态/哈希中,将您的网站转变为全功能的 Web 2.0 应用程序:
http://www.balupton.com/projects/jquery-ajaxy

他们都提供了很棒的他们的演示页面上的文档解释了正在发生的事情和正在发生的事情。

以下是使用 jQuery History 的示例(取自演示站点):

// 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);
});

以及 jQuery Ajaxy 的示例(取自演示站点):

        '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;

如果您想获取查询字符串参数(因此 yoursite/page .html#page1?ab=1&ac=2)您可以使用:

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

所以请查看这些演示链接以查看它们的实际情况,以及所有安装和使用详细信息。

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.

浸婚纱 2024-09-14 07:08:06

如果您使用jquery,则可以以简单的方式做到这一点。只需使用 ajaxify 插件即可。它可以管理 ajax 页面的书签和许多其他东西。

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.

萌梦深 2024-09-14 07:08:06

检查一下,有些东西可能会对您有所帮助:

  1. 如何从 javascript 更改 URL:http://doet.habrahabr。 ru/blog/15736/
  2. 如何将应用状态打包到 url 中: http:// habrahabr.ru/blogs/javascript/92505/
  3. 方法描述:http://habrahabr .ru/blogs/webstandards/92300/

注意:所有文章都是俄语,因此要么 Google 翻译它们,要么只是查看代码并猜测详细信息。

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.

把回忆走一遍 2024-09-14 07:08:06

我尝试了很多包。 jQuery History 插件似乎是最完整的:

http://github.com/tkyk/jquery-历史插件

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文