帮助window.history.pushState

时间:2011-08-10 08:41:28

标签: jquery pushstate

我需要语法帮助。

我的网站使用AJAX在#board div中加载博客帖子,然后点击#close关闭它。当我加载帖子时,网址就像http://www.visualise.ca/#!/anne-au-cherry一样,当我关闭帖子时,我想回到http://www.visualise.ca/。以下内容为我http://www.visualise.ca/#/

$("#close").live("click", function(event) {
    $("#board").slideUp("slow");
    window.location.hash = "#/";
    window.history.pushState(null,null,site_url+"/");
    return false;
});

1)有人可以帮忙吗?

2)如果浏览器不支持html5怎么办?

非常感谢您的时间和帮助。

更新:此操作,我的'site_url'变量中存在拼写错误。

1 个答案:

答案 0 :(得分:4)

PushState不是对哈希的操作。如果你想要它< html5兼容你需要使用哈希。

pushState正在更改网址而不更改网页:

如果您将历史记录视为数组,history = [];

您打开浏览器的空首页并转到page1.html

现在历史记录= ['page1.html']

如果您使用网址page2.html从page1.html触发pushState,则历史记录现在为['page1.html','page2.html'],地址栏显示为page2.html。

如果浏览器不支持pushState,它什么都不做。所以对你的例子来说:

$("#close").live("click", function(event) {
    $("#board").slideUp("slow");
    window.history.pushState(null, null, site_url+"/");
    return false;
});

当你加载你的ajax:

window.history.pushState(null,null,site_url + "/" + ajax_url);

如果您想使用哈希操作,可以执行以下操作:

$("#close").live("click", function(event) {
    $("#board").slideUp("slow");
    window.location.href = "#/"
    return false;
});

当你加载你的ajax:

window.location.href = "#/" + ajax_url

如果您正在使用pushState,请注意url可以在您没有的子文件夹中结束操作,因此您需要某种.htaccess重写代码

相关问题