我正在创建一个照片库,希望能够在浏览照片时更改查询字符串和标题.

我正在寻找的行为经常出现在一些连续/无限页面的实现中,当你向下滚动时,查询字符串会不断增加页码(http://x.com?page=4)等..这在理论上应该很简单,但我希望在主流浏览器中使用安全的东西.

I found this great post, and was trying to follow the example with window.history.pushstate, but that doesn't seem to be working for me. And I'm not sure if it is ideal because I don't really care about modifying the browser history.

I just want to be able to offer the ability to bookmark the currently viewed photo, without reloading the page every time the photo is changed.

Here is an example of infinite page that modifies query string: http://tumbledry.org/

UPDATE发现了这种方法:

window.location.href = window.location.href + '#abc';

推荐答案

If you are looking for Hash modification, your solution works ok. However, if you want to change the query, you can use the pushState, as you said. Here it is an example that might help you to implement it properly. I tested and it worked fine:

if (history.pushState) {
    var newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?myNewUrlQuery=1';
    window.history.pushState({path:newurl},'',newurl);
}

它不会重新加载页面,但只允许您更改URL查询.您将无法更改协议或主机值.当然,它需要能够处理HTML5历史API的现代浏览器.

有关更多信息:

http://diveintohtml5.info/history.html

https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history

Jquery相关问答推荐

如何在光滑的轮播上添加进度条

jQuery .append() 创建两个元素而不是一个

Sheets从多张sheet中导入range匹配数据

如何在 bootstrap 模式关闭并重新打开时重置 bootstrap 模式?

jquery清除输入默认值

一次替换多个字符串

如何在jQuery中 Select 具有特定ID的所有元素?

如何使用 jQuery 获取 div 的当前类?

验证外部脚本是否已加载

如果不是 jQuery,Javascript 中的美元符号是什么

如何使用 Twitter Bootstrap 自动关闭alert

测试空 jQuery Select 结果

删除 Javascript 中的所有多个空格并替换为单个空格

使用 jQuery 获取选中复选框的值

页面加载后如何执行jquery代码?

javascript,是否有像 isArray 这样的 isObject 函数?

HTMLCollection、NodeLists 和对象数组之间的区别

自调用函数前的分号?

粘性侧边栏:向下滚动时固定在底部,向上滚动时固定在顶部

如何让 jQuery 等到效果完成?