Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html4 browsers does not support history.pushState and history.replaceState methods of History API from HTML5

I am working on an ember application (using ember-1.0.pre.js). And I'm trying to provide cross browser compatibility on IE8.

The issue is with url generate after every transition, it seems incorrect/falsy to user. Let say I hit the url like the_ domain_name/sell/new which initially land me to on sell page of our application. and then i tries to transit a new state called "Purchase" which will land me on purchase page of our application.

The new state transition generates a URL the_ domain_name/sell/new#/offers/purchase?&suid=1365149991779013736531657156165 in IE8 addressbar instead of the domain_name/offers/purchase.

Note: the_domain_name = http://www.example.com

The generated url includes two incorrect things,

  1. The initial prefix "/sell/new#".

  2. The parameter "?&_suid=1365149991779013736531657156165" in query string of url.

I tried to figure out the issue and found that HTML4 browsers does not supports pushState and replaceState methods from History API from HTML5. How can i provide the support on IE8 Can anyone help me on this?

like image 495
Khajan Singh Avatar asked Dec 21 '22 07:12

Khajan Singh


1 Answers

I suggest History.js as polyfill for browsers not support History API: https://github.com/browserstate/history.js

It is working in:

HTML5 Browsers:

  • Firefox 4+
  • Chrome 8+
  • Opera 11.5
  • Safari 5.0+
  • Safari iOS 4.3+

HTML4 Browsers:

  • IE 6, 7, 8, 9
  • Firefox 3
  • Opera 10, 11.0
  • Safari 4
  • Safari iOS 4.2, 4.1, 4.0, 3.2

Add jquery.history.js & Register a history.js location handler into you Ember App.

Here are the parts I modified from original Ember.HistoryLocation ( Full code )

(function() {
  var get = Ember.get, set = Ember.set;
  var popstateFired = false;
  Ember.HistoryJsLocation = Ember.Object.extend({
    initState: function() {
      this.replaceState(this.formatURL(this.getURL()));
      set(this, 'history', window.History);
    },
    getState: function() {
      return get(this, 'history').getState().state;
    },
    pushState: function(path) {
      History.pushState({ path: path }, null, path);
    },
    replaceState: function(path) {
      History.replaceState({ path: path }, null, path);
    }
  });
  Ember.Location.registerImplementation('historyJs', Ember.HistoryJsLocation);
})();

Then use this polyfill in your App:

App.Router.reopen({
  location: 'historyJs'
});
like image 87
inDream Avatar answered May 01 '23 22:05

inDream