帮助window.history.pushState [英] Help with window.history.pushState

查看:152
本文介绍了帮助window.history.pushState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要语法方面的帮助.

I need help with syntax.

我的网站使用AJAX在#board div中加载博客文章,然后单击#close将其关闭.当我加载帖子时,URL变成这样 http://www.visualise.ca/#!/anne-au-cherry ,我想回到 http://www.visualise. ca/当我关闭帖子时.以下内容为我 http://www.visualise.ca/#/

My site loads blog posts within the #board div using AJAX and I close it by clicking #close. When I load a post the url becomes like this http://www.visualise.ca/#!/anne-au-cherry and I would like to come back to http://www.visualise.ca/ when I close the post. The following gives me 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)有人可以帮忙吗?

1) Can someone help please ?

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

2) What if the browser doesn't support html5 ?

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

Many thanks for your time and help.

更新:此工作,我的"site_url"变量中有错字.

推荐答案

PushState不能对哈希进行操作.如果您希望它是< html5兼容,您需要使用哈希.

PushState is not operation over the hash. If you want it to be < html5 compatible you need to use hash.

pushState在不更改页面的情况下更改了网址:

pushState is changing the url without changing page:

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

If you see the history as an array, history = [];

您打开浏览器的空白首页,然后转到page1.html

You open your browser's empty frontpage and go to page1.html

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

now history is = ['page1.html'].

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

If you fires pushState from page1.html with the url page2.html the history is now ['page1.html','page2.html'] and the address bar shows page2.html.

如果浏览器不支持pushState,则不执行任何操作.因此,对于您的示例:

If the browser dosn't support pushState it does nothing. So for your example:

$("#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);


如果要使用哈希运算,可以执行以下操作:


If you want to operate with hash you can do something like this:

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

当您加载ajax时:

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

如果您使用的是pushState,请注意url可以终止您没有的子文件夹中的操作指向,因此您需要某种.htaccess重写代码

If you are using pushState be aware of the urls can end op pointing in subfolders you dont have and therefore you need some kind of .htaccess rewrite code

这篇关于帮助window.history.pushState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆