Hack to keep history.pushState ie8-9

独自空忆成欢 提交于 2020-01-15 09:01:10

问题


On most modern browsers, I'm able to use:

history.pushState({}, 'Our Work','/url/path/');

Obviously IE doesn't support this, but I'm wondering why my simple hack doesn't work?

history = {
    pushState : function(state,title,url) {
        window.location = url;
    }
};

I've also tried:

window.history = {
    pushState : function(state,title,url) {
        window.location = url;
    }
};

But I get a 'member not found' attribute.

Is this even possible with IE8?

I really didn't want to include a whole library for this simple hack, it's weird though because adding:

if (!window.console) {
    console = {
        log: function() {},
        error: function() {}
    };
}

Fixes my console logs...

Any help would be great!


回答1:


That is because the history object does exist and it cannot be completely replaced.

But you can add additional methods to it

so setting

history.pushState = function(state,title,url){alert(url);};

will do the trick.



来源:https://stackoverflow.com/questions/27139571/hack-to-keep-history-pushstate-ie8-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!