forked from jquery-archive/jquery-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first pass at implementing pushstate support from an external file.
- Loading branch information
scottjehl
committed
Aug 5, 2011
1 parent
f789706
commit e564eb1
Showing
4 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); |