pushState & popState: Safari 5 not returning window.history.state

I can't seem to access the window.history.state object when my popState is triggered in Safari 5 (the page goes back). This code works perfectly in Chrome 19 and Firefox 12.

window.onload = function() {

     window.setTimeout(function() {

          var original = window.location.href;

          window.addEventListener("popstate", function(e) {

                    if (window.history.state !== null) {
                        var type = window.history.state.type;
                        var page = window.history.state.page;
                        //custom Ajax function below
                        getContent({type:type, page:page, category: category}, '/' + type + '/' + page);
                    } else {
                        window.location = original;
                    }

            }, false);

     }, 1);
}

console.log(window.history.state) in Safari 5 returns 'undefined'.

Answers:

Answer

window.history.state is only available in some browsers. It's not part of the spec per se.

Feature           ? Chrome  ? Firefox (Gecko)  ?  IE  ? Opera  ? Safari
???????????????????????????????????????????????????????????????????????
replace/pushState ?   5     ?    4.0 (2.0)     ?  10  ? 11.50  ?  5.0
history.state     ?  18     ?    4.0 (2.0)     ?  10  ? 11.50  ?  ---
Answer

So if history.state is supported then the value will be null(or an object if the browser is remembering the state), otherwise it will be undefined. And I ended up with this code to check whether it's supported or not

(window.history && history.pushState && history.state !== undefined) 
? true : false;

Tested in latest Chrome, Safari 5, 6, IE 9

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.