有关 window.history.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”变量中有一个拼写错误。
I need help with syntax.
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) Can someone help please ?
2) What if the browser doesn't support html5 ?
Many thanks for your time and help.
UPDATE: THIS WORKS, there was a typo in my 'site_url' variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PushState 不是对哈希的操作。如果你希望它是 < html5兼容你需要使用hash。
PushState 正在更改 url 而不更改页面:
如果您将历史记录视为数组,
history = [];
您打开浏览器的空首页并转到 page1.html
现在历史记录 =
[ 'page1.html']
。如果您使用 URL page2.html 从 page1.html 触发 PushState,历史记录现在为
['page1.html','page2.html']
并且地址栏显示 page2.html。如果浏览器不支持pushState,它什么也不做。因此,对于您的示例:
当您加载 ajax 时:
如果您想使用哈希进行操作,您可以执行以下操作:
当您加载 ajax 时:
如果您使用 PushState 请注意 url 可能会结束指向您的子文件夹中的操作没有,因此您需要某种 .htaccess 重写代码
PushState is not operation over the hash. If you want it to be < html5 compatible you need to use hash.
pushState is changing the url without changing page:
If you see the history as an array,
history = [];
You open your browser's empty frontpage and go to page1.html
now history is =
['page1.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.If the browser dosn't support pushState it does nothing. So for your example:
And when you load your ajax:
If you want to operate with hash you can do something like this:
And when you load your ajax:
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