【问题标题】:AJAX Hotlinking/back/refresh capability using .load使用 .load 的 AJAX 热链接/返回/刷新功能
【发布时间】:2014-04-04 20:00:02
【问题描述】:

下面是我的 AJAX 代码将 DIV 从外部 HTML 文件加载到目标 DIV 到另一个 HTML 页面上,任何人都可以建议如何使加载内容的页面具有返回/刷新/热链接功能,保持 DIV 加载在页?

$(document).ready(function(){
    $(document).on('click', '#about2', function() {
        $("#content").load("content.html #about", function() {
        $.getScript("slides.js", function() {
        $(window).scrollTop(0);
        });
        });
    });

    $(document).on('click', '#process2', function() {
        $("#content").load("content.html #process", function() {
        $(window).scrollTop(0);
        });
    });

    $(document).on('click', '#materials2', function() {
        $("#content").load("content.html #materials", function() {
        $(window).scrollTop(0);
        });
    });   

    $(document).on('click', '#radio2', function() {
        $("#content2").load("content.html #radiofriendly", function() {
        $(window).scrollTop(0);
        });
    });
    });

基本上,我想:

http://crookedcartoon.co.uk/print.html#process

直接链接到加载了#process DIV 的页面,允许刷新、后退、前进浏览器功能。有什么建议么?目前它只是链接到 http://crookedcartoon.co.uk/print.html 并加载了原始 DIV。

在此先感谢,JQuery 确实有问题。

编辑**

$(document).ready(function(){
$(document).on('click', '#about2',function ParseURL() {
    switch(location.hash.split("&")[0]) {
        case '#about':
            $("#content").load("content.html #about", function() {
                $(window).scrollTop(0);
            });
            break;

$(document).on('click', '#process2',function ParseURL() {
    switch(location.hash.split("&")[0]) {
        case '#process':
            $("#content").load("content.html #process", function() {
                $(window).scrollTop(0);
            });
            break;

$(document).on('click', '#materials2',function ParseURL() {
    switch(location.hash.split("&")[0]) {
        case '#process':
            $("#content").load("content.html #materials", function() {
                $(window).scrollTop(0);
            });
            break;

$(document).on('click', '#pricing2',function ParseURL() {
    switch(location.hash.split("&")[0]) {
        case '#process':
            $("#content").load("content.html #pricing", function() {
                $(window).scrollTop(0);
            });
            break;

         default:
    }
}

$(window).bind("pageshow", function(event) {
    if (event.originalEvent.persisted) {
        ParseURL();
    }
});

$(document).ready(function ($) {
    ParseURL();
}); 

【问题讨论】:

  • 这就是它的工作原理,当您在事件上动态加载内容时,当您离开站点时,该内容就消失了。有一些方法可以保持持久存储,但要跟踪您所做的所有动态更改,并让它们在页面重新加载时重新出现,这将是一项相当艰巨的任务。

标签: jquery ajax browser


【解决方案1】:

使用 hashchange 事件检测 URL 修改。这将允许您在加载后更新 DIV。当你使用 back 和 forward 时,会有一个事件。使用它来查看哈希,并适当地加载内容 DIV。

这是一个很棒的 jQuery 插件,它通过示例支持您想要的功能:Ben Alman Hashchange plugin

就目前而言,当您从左栏中选择一个链接时,您的页面会正确更新,但在使用历史记录时不会更新。

将这些添加到与 print.html 关联的 javascript 中,而不是在 content.html 中!!!我相信你称它为 core.js?

//You will need one entry in this switch statement for *each* hash value: #process, #materials, #pricing, etc
function ParseURL() {
    switch(location.hash.split("&")[0]) {
        case '#materials':
            $("#content").load("content.html #materials", function() {
                $(window).scrollTop(0);
            });
            break;
        case '#process':
            $("#content").load("content.html #process", function() {
                $(window).scrollTop(0);
            });
            break;
         default:
    }
}

//This should update on history button use
$(window).bind("pageshow", function(event) {
    if (event.originalEvent.persisted) {
        ParseURL();
    }
});

//Using ParseURL inside your ready function will allow linking directly to a sub page
//This allows those going to the URL:
// http://crookedcartoon.co.uk/print.html#pricing directly will should display the 
// pricing information in the content DIV.
$(document).ready(function ($) {
    ParseURL();
}); 

【讨论】:

  • 谢谢,上面的代码中如何成功使用它有什么提示吗?我之前尝试过使用这个插件,但我不记得它成功了。
  • 此答案中的代码不需要插件。它应该按原样在您的页面上运行。
  • 另外,bbq 插件可能不是你想要的。它是更简约的 jQuery hashchange 插件的更重版本。
  • 使用上面输入的代码查看下面的答案,但是如果有人能看到它是如何出错的,它不起作用。如果你去页面,它不起作用,它需要一个插件来使用它,我一直都知道这么多,只是没有使用它。我会研究 Onhashexchange 插件,我也尝试过,但没有成功。
  • 我看到你使用了 BBQ 插件。更新的插件是:github.com/cowboy/jquery-hashchange/raw/v1.3/…
猜你喜欢
  • 2011-10-13
  • 1970-01-01
  • 2019-10-10
  • 2016-10-14
  • 2018-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多