我要做的最好例子是YouTube的导航系统。如果你点击一个视频链接,页面顶部会出现一个红色条形指示器,显示请求页面加载了多少。当请求页面加载完成后,它立即替换前一页的内容,视频开始加载。为了使这个过程尽可能无缝,有几个事情正在发生。
- 修改URL以匹配新页面。
- 没有重定向(这很难解释)。
- 新页面被添加到历史记录中。
- 前进和后退导航无缝运作。
我用以下方式修改了URL:
window.history.pushState("object or string", "Title", "/new-url");
我尝试使用以下方式加载所请求的页面:
$.("html").load("newPage.php", function(){});
这将用新页面的代码替换整个先前页面的代码。这存在几个问题:
Back and forth navigation works with the URL, but an event handler must detect this and load the proper page. I used this:
$(window).on('popstate', function() { $('html').load("../"+window.location.pathname, {page:"account"}); });
- Using
$.load()
can't(shouldn't) load any new scripts tags script tags becauseSynchronous XMLHttpRequest on the main thread is deprecated
我想要做的:
- 使用JQuery和AJAX无需重定向即可无缝加载页面
- 新页面能够有新的脚本和PHP标签(可能无法使用PHP,需要使用AJAX来复制此功能)。
来源: