最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the 'data' in window.history.replaceState - Stack Overflow

programmeradmin3浏览0评论

We are working on a project using window.history.replacestate. Unfortunately we aren't very good at javascript.

Essentially we are using it to replace any number of shortlinks to the 'root' link. For example :

domain/fJfk8
domain/9dkDl
domain/fjgdD

would all 'appear' as :

domain/nice_url

It doesn't have to work so we'll put the javascript in the page and in the browsers it does then great and if it doesn't, they get the shortcode.

Note : we don't want history !

So we are just using :

window.history.replaceState('Object','Nice URL Title', '/nice_url');

The question is that this appears to work but we don't understand the 'Object' (data) part.

What exactly is it ?

We are working on a project using window.history.replacestate. Unfortunately we aren't very good at javascript.

Essentially we are using it to replace any number of shortlinks to the 'root' link. For example :

domain./fJfk8
domain./9dkDl
domain./fjgdD

would all 'appear' as :

domain./nice_url

It doesn't have to work so we'll put the javascript in the page and in the browsers it does then great and if it doesn't, they get the shortcode.

Note : we don't want history !

So we are just using :

window.history.replaceState('Object','Nice URL Title', '/nice_url');

The question is that this appears to work but we don't understand the 'Object' (data) part.

What exactly is it ?

Share asked Sep 19, 2011 at 8:20 Mike FredericoMike Frederico 731 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You can set the Object parameter to arbritary data, which will be available in the state parameter of the event object which is available in the popstate event.

In other words, you can set it to whatever you want, to help you restore the webpage to the desired state when the user navigates through their history.

For more information, see the MDC documentation on window.history.

window.history.replaceState({
    foo: 'bar'
}, 'Nice URL Title', '/nice_url');

window.onpopstate = function (e) {
    if (typeof e.state == "object" && e.state.foo == "bar") {
        alert("Blah blah blah");
    }
};

window.history.go(-1);
发布评论

评论列表(0)

  1. 暂无评论