history.replaceState() example?

后端 未结 8 901
礼貌的吻别
礼貌的吻别 2020-11-29 20:14

Can any one give a working example for history.replaceState? This is what w3.org says:

history . replaceState(data, title [, url ] )

相关标签:
8条回答
  • 2020-11-29 20:28

    The second argument Title does not mean Title of the page - It is more of a definition/information for the state of that page

    But we can still change the title using onpopstate event, and passing the title name not from the second argument, but as an attribute from the first parameter passed as object

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

    0 讨论(0)
  • 2020-11-29 20:30

    Suppose https://www.mozilla.org/foo.html executes the following JavaScript:

    const stateObj = { foo: 'bar' };
    
    history.pushState(stateObj, '', 'bar.html');
    

    This will cause the URL bar to display https://www.mozilla.org/bar2.html, but won't cause the browser to load bar2.html or even check that bar2.html exists.

    0 讨论(0)
  • 2020-11-29 20:31

    Here is a minimal, contrived example.

    console.log( window.location.href );  // whatever your current location href is
    window.history.replaceState( {} , 'foo', '/foo' );
    console.log( window.location.href );  // oh, hey, it replaced the path with /foo
    

    There is more to replaceState() but I don't know what exactly it is that you want to do with it.

    0 讨论(0)
  • 2020-11-29 20:32

    Indeed this is a bug, although intentional for 2 years now. The problem lies with some unclear specs and the complexity when document.title and back/forward are involved.

    See bug reference on Webkit and Mozilla. Also Opera on the introduction of History API said it wasn't using the title parameter and probably still doesn't.

    Currently the 2nd argument of pushState and replaceState — the title of the history entry — isn't used in Opera's implementation, but may be one day.

    Potential solution

    The only way I see is to alter the title element and use pushState instead:

    document.getElementsByTagName('title')[0].innerHTML = 'bar';
    window.history.pushState( {} , 'bar', '/bar' );
    
    0 讨论(0)
  • 2020-11-29 20:34

    look at the example

    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);
    

    and search location.hash;

    0 讨论(0)
  • 2020-11-29 20:40

    According to MDN History doc
    There is clearly said that second argument is for future used not for now. You are right that second argument is deal with web-page title but currently it's ignored by all major browser.

    Firefox currently ignores this parameter, although it may use it in the future. Passing the empty string here should be safe against future changes to the method. Alternatively, you could pass a short title for the state to which you're moving.

    0 讨论(0)
提交回复
热议问题