开发者

Overriding history.pushState leads to error in opera 11

I'm injecting the following code into a webpage via a greasemonkey script/opera extension to trap the history.pushState command, so I can do some processing whenever it's fired and still allow the pushState command to continue afterwards.

(function(history){
  var pushState = history.pushState;
  history.pushState = function(state) {
       if (typeof history.onpushstate == "function") {
          history.onpushstate({state: state});
        }
        alert('pushstate called')
        return pushState.apply(history, arguments);
  }
})(window.history);

the code works fine in FF4 and Chrome, but in Opera 11, I get the 开发者_JS百科following error, if the page calls a history.replaceState command:

Uncaught exception: TypeError: 'window.history.replaceState' is not a function

Does anyone know how I can fix the above code to work with Opera as well as Chrome and Firefox?


In Opera 11.00, Revision 1156, the history API supported are these

>>> history.
back, current, forward, go, length, navigationMode

The full HTML5 history API is not yet covered by Opera 11.00. In general if you would like to discover, explore what is supported, you can easily use the console mode of dragonfly, the Web developer tool.


According to When can I use … Opera doesn't support the History API yet, so that's why you get that exception.


I figured out the solution, just check for history.replacestate before executing the above code, if it doesn't exist, don't execute the code, simple.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜