Skip to content

Commit c8a002b

Browse files
committed
added hash change fallback to push state for IE support
1 parent 0c48f02 commit c8a002b

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

lib/PushstateHelper.js

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ From 2010 till ∞
1515
typecode-js v 0.1
1616
*/
1717

18+
// This module uses jQuery Hashchange (http://benalman.com/projects/jquery-hashchange-plugin/)
19+
// as a fallback to emulate pushstate behavior on browsers that don't support pushstate.
20+
// The hashchange plugin subsequently requires jQuery Migrate (https://github.com/jquery/jquery-migrate)
21+
// if being used with jQuery version 1.9 +
22+
1823
define(['jquery'], function($) {
1924

2025
var window = this,
@@ -31,23 +36,34 @@ define(['jquery'], function($) {
3136

3237
internal = {
3338
name: 'mod.PushstateHelper',
34-
previous_path: window.location.pathname
39+
previous_path: o.use_hash ?
40+
window.location.hash.replace('#', '') : window.location.pathname
3541
};
3642

3743
fn = {
3844
init: function() {
3945
$(document).on('click', '.js-use-pushstate, .js-simulate-pushstate, .js-do-popstate', handlers.doc_click);
40-
$(window).on('popstate', handlers.popstate).on('pushstate', handlers.pushstate).on('simulate-pushstate', handlers.simulate_pushstate);
41-
o.app.events.on('app.featuresInitialized', handlers.features_initialized);
42-
if(o.use_hash){
43-
fn.statechange(window.location.hash);
46+
$(window)
47+
.on('popstate', handlers.popstate)
48+
.on('pushstate', handlers.pushstate)
49+
.on('simulate-pushstate', handlers.simulate_pushstate);
50+
51+
if (o.use_hash) {
52+
require(['jquery_migrate', 'hashchange'], function() {
53+
$(window).on('hashchange', handlers.hashchange);
54+
fn.statechange(window.location.hash);
55+
});
4456
} else {
4557
fn.statechange(window.location.pathname);
4658
}
4759
},
4860
statechange: function(pathname, data){
4961
var i, path_components, position, _event_data;
5062

63+
if (o.use_hash) {
64+
pathname = pathname.replace('#', '');
65+
}
66+
5167
path_components = PushstateHelper.get_path_components(pathname);
5268

5369
_event_data = {
@@ -69,25 +85,23 @@ define(['jquery'], function($) {
6985
};
7086

7187
handlers = {
72-
features_initialized: function(e,d){
73-
//fn.statechange(window.location);
74-
},
7588
doc_click: function(e, d) {
76-
var _href, _data;
89+
var $t, _href, _data;
7790
e.preventDefault();
78-
_href = $(this).attr('HREF');
79-
_data = $(this).data();
80-
if($(this).hasClass('js-use-pushstate')){
91+
$t = $(this);
92+
_href = $t.attr('HREF');
93+
_data = $t.data();
94+
if($t.hasClass('js-use-pushstate')){
8195
if(o.use_hash){
8296
window.location.hash = _href;
8397
fn.statechange(window.location.hash, _data);
8498
} else {
8599
history.pushState(null, null, _href);
86100
fn.statechange(window.location.pathname, _data);
87101
}
88-
} else if($(this).hasClass('js-simulate-pushstate')) {
102+
} else if($t.hasClass('js-simulate-pushstate')) {
89103
fn.statechange(_href, _data);
90-
} else if($(this).hasClass('js-do-popstate')) {
104+
} else if($t.hasClass('js-do-popstate')) {
91105
history.back();
92106
}
93107
},
@@ -108,14 +122,19 @@ define(['jquery'], function($) {
108122
history.pushState(null, null, d.pathname);
109123
}
110124
}
111-
if(!d.prevent_propagation){
125+
if(!d || !d.prevent_propagation){
112126
fn.statechange(d.pathname, d);
113127
}
114128
},
115129
simulate_pushstate: function(e, d) {
116-
if(!d.prevent_propagation){
130+
if(!d || !d.prevent_propagation){
117131
fn.statechange(d.pathname, d);
118132
}
133+
},
134+
hashchange: function(e, d) {
135+
if (!d || !d.prevent_propagation) {
136+
fn.statechange(window.location.hash);
137+
}
119138
}
120139
};
121140

@@ -127,7 +146,6 @@ define(['jquery'], function($) {
127146

128147
PushstateHelper.get_path_components = function(path){
129148
var components, position;
130-
components = [];
131149
components = path.split('/');
132150
position = components.length;
133151
while(position--){

requirepaths.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require = {
22
paths: {
33
'jquery': 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min',
4+
'jquery_migrate': 'http://code.jquery.com/jquery-migrate-1.2.1.js',
5+
'hashchange': 'thirdparty/jquery-migrate-1.2.1.min',
46

57
'NIanim': 'lib/anim',
68
'NIAnimator': 'lib/Animator',
@@ -38,5 +40,9 @@ require = {
3840
'NIvalidation': 'lib/validation',
3941

4042
'NIPushstateHelper': 'lib/PushstateHelper'
43+
},
44+
shim: {
45+
jquery_migrate: ['jquery'],
46+
hashchange: ['jquery', 'jquery_migrate']
4147
}
4248
};

thirdparty/jquery-migrate-1.2.1.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

thirdparty/jquery.ba-hashchange.min.js

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)