Hack to keep history.pushState ie8-9

2019-07-19 11:03发布

问题:

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.