11/*!
2- * History API JavaScript Library v4.1.5
2+ * History API JavaScript Library v4.1.6
33 *
44 * Support: IE8+, FF3+, Opera 9+, Safari, Chrome and other
55 *
1111 * http://www.opensource.org/licenses/mit-license.php
1212 * http://www.gnu.org/licenses/gpl.html
1313 *
14- * Update: 2014-05-14 22:01
14+ * Update: 2014-05-15 17:13
1515 */
1616( function ( factory ) {
17- if ( typeof self === 'object' && self . history ) {
18- factory ( self ) ;
19- if ( typeof define === 'function' && define [ 'amd' ] ) {
20- define ( "html5-history-api" , function ( ) {
21- return self . history ;
22- } ) ;
23- }
17+ if ( typeof define === 'function' && define [ 'amd' ] ) {
18+ // https://github.com/devote/HTML5-History-API/issues/57#issuecomment-43133600
19+ define ( typeof document !== "object" || document . readyState === "complete" ? [ ] : "html5-history-api" , factory ) ;
20+ } else {
21+ factory ( ) ;
2422 }
25- } ) ( function ( window ) {
26- // Prevent the code from running if there is no window.history object
27- if ( ! window . history ) return ;
23+ } ) ( function ( ) {
24+ // Define global variable
25+ var global = ( typeof window === 'object' ? window : this ) || { } ;
26+ // Prevent the code from running if there is no window.history object or library already loaded
27+ if ( ! global . history || "emulate" in global . history ) return global . history ;
2828 // symlink to document
29- var document = window . document ;
29+ var document = global . document ;
3030 // HTML element
3131 var documentElement = document . documentElement ;
3232 // symlink to constructor of Object
33- var Object = window [ 'Object' ] ;
33+ var Object = global [ 'Object' ] ;
3434 // symlink to JSON Object
35- var JSON = window [ 'JSON' ] ;
35+ var JSON = global [ 'JSON' ] ;
3636 // symlink to instance object of 'Location'
37- var windowLocation = window . location ;
37+ var windowLocation = global . location ;
3838 // symlink to instance object of 'History'
39- var windowHistory = window . history ;
39+ var windowHistory = global . history ;
4040 // new instance of 'History'. The default is a reference to the original object instance
4141 var historyObject = windowHistory ;
4242 // symlink to method 'history.pushState'
5454 // prefix for the names of events
5555 var eventNamePrefix = '' ;
5656 // String that will contain the name of the method
57- var addEventListenerName = window . addEventListener ? 'addEventListener' : ( eventNamePrefix = 'on' ) && 'attachEvent' ;
57+ var addEventListenerName = global . addEventListener ? 'addEventListener' : ( eventNamePrefix = 'on' ) && 'attachEvent' ;
5858 // String that will contain the name of the method
59- var removeEventListenerName = window . removeEventListener ? 'removeEventListener' : 'detachEvent' ;
59+ var removeEventListenerName = global . removeEventListener ? 'removeEventListener' : 'detachEvent' ;
6060 // String that will contain the name of the method
61- var dispatchEventName = window . dispatchEvent ? 'dispatchEvent' : 'fireEvent' ;
61+ var dispatchEventName = global . dispatchEvent ? 'dispatchEvent' : 'fireEvent' ;
6262 // reference native methods for the events
63- var addEvent = window [ addEventListenerName ] ;
64- var removeEvent = window [ removeEventListenerName ] ;
65- var dispatch = window [ dispatchEventName ] ;
63+ var addEvent = global [ addEventListenerName ] ;
64+ var removeEvent = global [ removeEventListenerName ] ;
65+ var dispatch = global [ dispatchEventName ] ;
6666 // default settings
6767 var settings = { "basepath" : '/' , "redirect" : 0 , "type" : '/' } ;
6868 // key for the sessionStorage
9898 * See https://github.com/devote/HTML5-History-API/issues/29
9999 */
100100 var fastFixChrome = function ( method , args ) {
101- var isNeedFix = window . history !== windowHistory ;
101+ var isNeedFix = global . history !== windowHistory ;
102102 if ( isNeedFix ) {
103- window . history = windowHistory ;
103+ global . history = windowHistory ;
104104 }
105105 method . apply ( windowHistory , args ) ;
106106 if ( isNeedFix ) {
107- window . history = historyObject ;
107+ global . history = historyObject ;
108108 }
109109 } ;
110110
116116 * @type {Object }
117117 */
118118 var historyDescriptors = {
119+ /**
120+ * Setting library initialization
121+ *
122+ * @param {null|String } [basepath] The base path to the site; defaults to the root "/".
123+ * @param {null|String } [type] Substitute the string after the anchor; by default "/".
124+ * @param {null|Boolean } [redirect] Enable link translation.
125+ */
126+ "setup" : function ( basepath , type , redirect ) {
127+ settings [ "basepath" ] = ( '' + ( basepath == null ? settings [ "basepath" ] : basepath ) )
128+ . replace ( / (?: ^ | \/ ) [ ^ \/ ] * $ / , '/' ) ;
129+ settings [ "type" ] = type == null ? settings [ "type" ] : type ;
130+ settings [ "redirect" ] = redirect == null ? settings [ "redirect" ] : ! ! redirect ;
131+ } ,
119132 /**
120133 * @namespace history
121134 * @param {String } [type]
122135 * @param {String } [basepath]
123136 */
124137 "redirect" : function ( type , basepath ) {
125- settings [ "basepath" ] = basepath = ( '' + ( basepath == null ?
126- settings [ "basepath" ] : basepath ) ) . replace ( / (?: ^ | \/ ) [ ^ \/ ] * $ / , '/' ) ;
127- settings [ "type" ] = type = type == null ? settings [ "type" ] : type ;
128- if ( window . top == window . self ) {
138+ historyObject [ 'setup' ] ( basepath , type ) ;
139+ basepath = settings [ "basepath" ] ;
140+ if ( global . top == global . self ) {
129141 var relative = parseURL ( null , false , true ) . _relative ;
130142 var path = windowLocation . pathname + windowLocation . search ;
131143 if ( isSupportHistoryAPI ) {
137149 path = path . replace ( / ( [ ^ \/ ] ) \? / , '$1/?' ) ;
138150 if ( ( new RegExp ( "^" + basepath , "i" ) ) . test ( path ) ) {
139151 windowLocation . replace ( basepath + '#' + path .
140- replace ( new RegExp ( "^" + basepath , "i" ) , type ) + windowLocation . hash ) ;
152+ replace ( new RegExp ( "^" + basepath , "i" ) , settings [ " type" ] ) + windowLocation . hash ) ;
141153 }
142154 }
143155 }
191203 */
192204 "location" : {
193205 set : function ( value ) {
194- window . location = value ;
206+ global . location = value ;
195207 } ,
196208 get : function ( ) {
197209 return isSupportHistoryAPI ? windowLocation : locationObject ;
421433 * and: http://stackoverflow.com/a/12976988/669360
422434 */
423435 try {
424- sessionStorage = window [ 'sessionStorage' ] ;
436+ sessionStorage = global [ 'sessionStorage' ] ;
425437 sessionStorage . setItem ( sessionStorageKey + 't' , '1' ) ;
426438 sessionStorage . removeItem ( sessionStorageKey + 't' ) ;
427439 } catch ( _e_ ) {
505517 }
506518
507519 // Browser refused to override the property, using the standard and deprecated methods
508- if ( ( ! isDefinedSetter || ! isDefinedGetter ) && object === window ) {
520+ if ( ( ! isDefinedSetter || ! isDefinedGetter ) && object === global ) {
509521 try {
510522 // save original value from this property
511523 var originalValue = object [ prop ] ;
514526 } catch ( _e_ ) {
515527 }
516528 // This rule for Internet Explorer 8
517- if ( 'execScript' in window ) {
529+ if ( 'execScript' in global ) {
518530 /**
519531 * to IE8 override the global properties using
520532 * VBScript, declaring it in global scope with
521533 * the same names.
522534 */
523- window [ 'execScript' ] ( 'Public ' + prop , 'VBScript' ) ;
524- window [ 'execScript' ] ( 'var ' + prop + ';' , 'JavaScript' ) ;
535+ global [ 'execScript' ] ( 'Public ' + prop , 'VBScript' ) ;
536+ global [ 'execScript' ] ( 'var ' + prop + ';' , 'JavaScript' ) ;
525537 } else {
526538 try {
527539 /**
658670 get : event === 'type' ? function ( ) {
659671 return eventType ;
660672 } : function ( ) {
661- return window ;
673+ return global ;
662674 }
663675 } ) ;
664676 }
665677 }
666678 // run function defined in the attributes 'onpopstate/onhashchange' in the 'window' context
667- ( ( eventType === 'popstate' ? window . onpopstate : window . onhashchange )
668- || emptyFunction ) . call ( window , eventObject ) ;
679+ ( ( eventType === 'popstate' ? global . onpopstate : global . onhashchange )
680+ || emptyFunction ) . call ( global , eventObject ) ;
669681 // run other functions that are in the list of handlers
670682 for ( var i = 0 , len = list . length ; i < len ; i ++ ) {
671- list [ i ] . call ( window , eventObject ) ;
683+ list [ i ] . call ( global , eventObject ) ;
672684 }
673685 return true ;
674686 } else {
753765 firePopState ( ) ;
754766 }
755767 // current event object
756- event = event || window . event ;
768+ event = event || global . event ;
757769
758770 var oldURLObject = parseURL ( lastURL , true ) ;
759771 var newURLObject = parseURL ( ) ;
821833 * @param {Event } e
822834 */
823835 function onAnchorClick ( e ) {
824- var event = e || window . event ;
836+ var event = e || global . event ;
825837 var target = anchorTarget ( event . target || event . srcElement ) ;
826838 var defaultPrevented = "defaultPrevented" in event ? event [ 'defaultPrevented' ] : event . returnValue === false ;
827839 if ( target && target . nodeName === "A" && ! defaultPrevented ) {
851863 var target = document . getElementById ( hash = ( hash || '' ) . replace ( / ^ # / , '' ) ) ;
852864 if ( target && target . id === hash && target . nodeName === "A" ) {
853865 var rect = target . getBoundingClientRect ( ) ;
854- window . scrollTo ( ( documentElement . scrollLeft || 0 ) , rect . top + ( documentElement . scrollTop || 0 )
866+ global . scrollTo ( ( documentElement . scrollLeft || 0 ) , rect . top + ( documentElement . scrollTop || 0 )
855867 - ( documentElement . clientTop || 0 ) ) ;
856868 }
857869 }
869881 var src = ( scripts [ scripts . length - 1 ] || { } ) . src || '' ;
870882 var arg = src . indexOf ( '?' ) !== - 1 ? src . split ( '?' ) . pop ( ) : '' ;
871883 arg . replace ( / ( \w + ) (?: = ( [ ^ & ] * ) ) ? / g, function ( a , key , value ) {
872- value = ( value || '' ) . replace ( / ^ ( 0 | f a l s e ) $ / , '' ) ;
873- settings [ key ] = key === 'basepath' ? value . replace ( / (?: ^ | \/ ) [ ^ \/ ] * $ / , '/' ) : value ;
884+ settings [ key ] = ( value || '' ) . replace ( / ^ ( 0 | f a l s e ) $ / , '' ) ;
874885 } ) ;
875886
876887 /**
879890 addEvent ( eventNamePrefix + 'hashchange' , onHashChange , false ) ;
880891
881892 // a list of objects with pairs of descriptors/object
882- var data = [ locationDescriptors , locationObject , eventsDescriptors , window , historyDescriptors , historyObject ] ;
893+ var data = [ locationDescriptors , locationObject , eventsDescriptors , global , historyDescriptors , historyObject ] ;
883894
884895 // if browser support object 'state' in interface 'History'
885896 if ( isSupportStateObjectInHistory ) {
902913 // is satisfied if the failed override property
903914 if ( o === historyObject ) {
904915 // the problem occurs in Safari on the Mac
905- window . history = historyObject = data [ i + 1 ] = n ;
916+ global . history = historyObject = data [ i + 1 ] = n ;
906917 }
907918 } ) ) {
908919 // if there is no possibility override.
916927 }
917928
918929 // create a repository for custom handlers onpopstate/onhashchange
919- if ( data [ i + 1 ] === window ) {
930+ if ( data [ i + 1 ] === global ) {
920931 eventsList [ prop ] = eventsList [ prop . substr ( 2 ) ] = [ ] ;
921932 }
922933 }
923934 }
924935 }
925936 }
926937
938+ // check settings
939+ historyObject [ 'setup' ] ( ) ;
940+
927941 // redirect if necessary
928942 if ( settings [ 'redirect' ] ) {
929943 historyObject [ 'redirect' ] ( ) ;
982996 /**
983997 * Replace the original methods on the wrapper
984998 */
985- window [ addEventListenerName ] = addEventListener ;
986- window [ removeEventListenerName ] = removeEventListener ;
987- window [ dispatchEventName ] = dispatchEvent ;
999+ global [ addEventListenerName ] = addEventListener ;
1000+ global [ removeEventListenerName ] = removeEventListener ;
1001+ global [ dispatchEventName ] = dispatchEvent ;
9881002
1003+ return historyObject ;
9891004} ) ;
0 commit comments