Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js cause bug only in IE7

I'm developping a web app with CodeIgniter on back-end and Backbone.js on front-end. I'm also using HTML5 Boilerplate as my start template.

I'm using Backbone's Controller and History as main navigation through my application. I've done it one time in the past and everything have work fine. The problem is, when i start hashchange event capture with Backbone.History.start() and click on a link example.com/#home, the hash change in the url, the event is fired but 2 seconds after, the hash is cleared from url and an javascript error is throw only in ie7.

I've take a look at source code and hashchange event is acheived in IE7 by creating an IFRAME running an interval to check hash value change.

Anyone had this weird bug before and know how to solve this?

like image 520
Dominic Mercier Avatar asked Feb 11 '11 20:02

Dominic Mercier


2 Answers

The right way to handle an #hash base application with Backbone seems to Backbone.history.saveLocation( hash ) and after Backbone.history.loadUrl() to enable Controller's routing.

Whish i knew this before... Have fun with this awesome MVC library :)

like image 67
Dominic Mercier Avatar answered Oct 09 '22 13:10

Dominic Mercier


The solution that I found to work was to use Ben Alman's hashchange plugin. Go to the start function in Backbone.History and replace the code of the start function with this.

start : function() {
    $(window).hashchange(this.checkUrl);
    return this.loadUrl();
}

And be sure to include the hashchange plugin file in your code.

like image 39
Kyle Buchanan Avatar answered Oct 09 '22 13:10

Kyle Buchanan