1

我正在寻找继续向 url 参数示例添加 id 的最佳方法。

http://example.com/1

所以说,如果我把它设置在一个间隔左右,它会是。

var i = 0;
setInterval(function() {

document.location.search = 'http://example.com/' + i++;

}, 1000);

我想要的结果是没有页面刷新的 url 的额外参数,所以它只会不断更新。

http://example.com/1
http://example.com/2
http://example.com/3
http://example.com/4

ETC...

请提供任何帮助。

4

4 回答 4

0

您想更改“片段标识符”,它是 url 的一部分,无需刷新页面即可更改。

于 2013-07-03T12:49:19.447 回答
0

这听起来并不合乎逻辑,但如果你真的需要这样做,你可以使用history.pushState

实现这一目标的最简单方法是:

history.pushState("", "", "/1");

请注意,IE 中的支持非常有限

于 2013-07-03T12:55:17.440 回答
0

有两种方法可以做到这一点。一种是使用 HTML5 History API,将是这样的代码:

var i = 0;
setInterval(function() {

history.pushState(null, document.title, 'example.com/' + i++);

}, 1000);

However, this will push it to the browser history and not all browsers support it. If I remember correctly there's a way to push it to url without pushing it to history, but read a bit about it to see if you can do it.

Or you can use hash codes in case you don't mind a little # over there, but it will probably annoy the visitor as it might bump the page to top every second, if you don't mind then this is your code :

var i = 0;
var url= window.location.href;
setInterval(function() {

window.location.href = url+"#"+i++;


}, 1000);

However this sounds like a bad idea anyway, so reconsider using it. I'm pretty sure what you're trying to achive can be done in a cleaner way.

于 2013-07-03T12:56:37.837 回答
0

This can now be done in Chrome, Safari, FF, IE10.

window.history.pushState(“object or string”, “Title”, “/new-url”);

In your case:

var i = 0;
setInterval(function() {

window.history.pushState(“”, “”,'http://example.com/' + i++);

}, 1000);

More information here:

http://spoiledmilk.com/blog/html5-changing-the-browser-url-without-refreshing-page/

于 2013-07-03T13:01:11.550 回答