问题
In one of my applications i use window.history.pushState()
method to update the window URL. But as IE < 10
does not support HTML5's history api, I was looking for an alternative. Answers to a lot of other questions on SO suggested to use history.js plugin.
From the documentation provided by the history.js plugin it's usage is not really clear. I have added the plugin to the <head></head>
section in my templates but on IE9 i am still receiving the error that says:
SCRIPT438: Object doesn't support property or method 'pushState'
params.js, line 37 character 5
the function that errors out is as follows
/**
* updates the window url according to the given parameters.
*/
function updateUrl(params) {
var path = window.location.pathname;
var url = $.param.querystring(path, params);
url = decodeURIComponent(new_url).replace("#", "", "g");
window.history.pushState(null, null, new_url);
}
回答1:
You need to initiatialize the History and State variables for it to work.
(function(window, undefined) {
// Define variables
var History = window.History,
State = History.getState();
// Bind statechange event
History.Adapter.bind(window, 'statechange', function() {
var State = History.getState();
});
})(window)
and now you can use pushState
method for all browsers as follows:
History.pushState(null, null, '?your=hash')
来源:https://stackoverflow.com/questions/19634513/using-history-js-with-ie9