Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Commit

Permalink
first pass at implementing pushstate support from an external file.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottjehl committed Aug 5, 2011
1 parent f789706 commit e564eb1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -45,6 +45,7 @@ JSFILES = js/jquery.ui.widget.js \
js/jquery.mobile.page.js \
js/jquery.mobile.core.js \
js/jquery.mobile.navigation.js \
js/jquery.mobile.navigation.pushstate.js \
js/jquery.mobile.transition.js \
js/jquery.mobile.degradeInputs.js \
js/jquery.mobile.dialog.js \
Expand Down
1 change: 1 addition & 0 deletions build.xml
Expand Up @@ -29,6 +29,7 @@
js/jquery.mobile.page.js,
js/jquery.mobile.core.js,
js/jquery.mobile.navigation.js,
js/jquery.mobile.navigation.pushstate.js,
js/jquery.mobile.transition.js,
js/jquery.mobile.degradeInputs.js,
js/jquery.mobile.dialog.js,
Expand Down
1 change: 1 addition & 0 deletions js/index.php
Expand Up @@ -11,6 +11,7 @@
'jquery.mobile.page.js',
'jquery.mobile.core.js',
'jquery.mobile.navigation.js',
'jquery.mobile.navigation.pushstate.js',
'jquery.mobile.transition.js',
'jquery.mobile.degradeInputs.js',
'jquery.mobile.dialog.js',
Expand Down
50 changes: 50 additions & 0 deletions js/jquery.mobile.navigation.pushstate.js
@@ -0,0 +1,50 @@
/*
* jQuery Mobile Framework : history.pushState support, layered on top of hashchange
* Copyright (c) jQuery Project
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*/
( function( $ ) {

// For now, let's Monkeypatch this onto the end of $.mobile._registerInternalEvents
var oldRegisterInternalEvents = $.mobile._registerInternalEvents;

$.mobile._registerInternalEvents = function(){

// Call previous function
oldRegisterInternalEvents();

// Initial href without hash becomes base for hash changes
var initUrl = location.href.split( "#" )[0].match( /[^\/]*\/\/[^\/]+(.*)/ ) && RegExp.$1,
// Begin with popstate listening disabled, since it fires at onload in chrome
popListeningEnabled = false;

$( window ).bind( "hashchange replacehash", function( e ) {
if( $.support.pushState ){
history.replaceState( { hash: location.hash || "#" + initUrl, title: document.title }, document.title, location.href.split( "#" )[ 1 ] );
}
});

// Handle popstate events the occur through history changes
$( window ).bind( "popstate", function( e ) {
if( popListeningEnabled ){
if( e.originalEvent.state ){
history.replaceState( e.originalEvent.state, e.originalEvent.state.title, initUrl + e.originalEvent.state.hash );
$( window ).trigger( "hashchange" );
}
}
});

// Replace the hash before pushstate listening is enabled
$( window ).trigger( "replacehash" );

// Enable pushstate listening after window onload
$( window ).load( function(){
setTimeout(function(){
popListeningEnabled = true;
}, 10 );
});

};

})( jQuery );

0 comments on commit e564eb1

Please sign in to comment.