How can I ignore [removed] on page load?

前端 未结 4 1075
一个人的身影
一个人的身影 2020-12-04 23:52

I\'m playing with window.onpopstate, and there is a thing that annoys me a little bit:

Browsers tend to handle the popstate event differe

4条回答
  •  悲哀的现实
    2020-12-05 00:41

    Check for boolean truth of event.state in popstate event handler:

    window.addEventListener('popstate', function(event) {
        if (event.state) {
            alert('!');
        }
    }, false);
    

    To ensure this will work, always specify a non-null state argument when calling history.pushState() or history.replaceState(). Also, consider using a wrapper library like History.js that provides consistent behavior across browsers.

提交回复
热议问题