Skip to content

Commit 8f75a87

Browse files
committed
fix error: Mismatched anonymous define()
1 parent 1a30586 commit 8f75a87

7 files changed

Lines changed: 182 additions & 154 deletions

File tree

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ npm install html5-history-api
1717
<head>
1818
<script type="text/javascript" src="/require.js"></script>
1919
<script type="text/javascript">
20-
requirejs.config({
21-
paths: {
22-
'html5-history-api': '/history'
23-
}
24-
});
25-
requirejs(['html5-history-api'], function() {
20+
requirejs(['/history'], function() {
2621
if (history.emulate) {
2722
console.log('In your browser is emulated HTML5-History-API');
2823
} else {

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "history",
33
"repo": "devote/HTML5-History-API",
44
"description": "HTML5 History API expansion for browsers not supporting pushState, replaceState",
5-
"version": "4.1.5",
5+
"version": "4.1.6",
66
"keywords": ["history", "pushState", "replaceState"],
77
"main": "history.js",
88
"scripts": ["history.js"],

history.iegte8.js

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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
*
@@ -11,32 +11,32 @@
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'
@@ -54,15 +54,15 @@
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
@@ -98,13 +98,13 @@
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

@@ -116,16 +116,28 @@
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) {
@@ -137,7 +149,7 @@
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
}
@@ -191,7 +203,7 @@
191203
*/
192204
"location": {
193205
set: function(value) {
194-
window.location = value;
206+
global.location = value;
195207
},
196208
get: function() {
197209
return isSupportHistoryAPI ? windowLocation : locationObject;
@@ -421,7 +433,7 @@
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_) {
@@ -505,7 +517,7 @@
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];
@@ -514,14 +526,14 @@
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
/**
@@ -658,17 +670,17 @@
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 {
@@ -753,7 +765,7 @@
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();
@@ -821,7 +833,7 @@
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) {
@@ -851,7 +863,7 @@
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
}
@@ -869,8 +881,7 @@
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|false)$/, '');
873-
settings[key] = key === 'basepath' ? value.replace(/(?:^|\/)[^\/]*$/, '/') : value;
884+
settings[key] = (value || '').replace(/^(0|false)$/, '');
874885
});
875886

876887
/**
@@ -879,7 +890,7 @@
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) {
@@ -902,7 +913,7 @@
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.
@@ -916,14 +927,17 @@
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']();
@@ -982,8 +996,9 @@
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

Comments
 (0)