Skip to content

Commit fc403a1

Browse files
Merge pull request victorjonsson#557 from enyce12/master
Use require to load scripts if in an AMD environment
2 parents fd89f88 + 4e63023 commit fc403a1

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

form-validator/jquery.form-validator.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,23 +1014,27 @@
10141014
$.formUtils.loadedModules[scriptUrl] = 1;
10151015
hasLoadedAnyModule = true;
10161016

1017-
// Load the script
1018-
script.type = 'text/javascript';
1019-
script.onload = moduleLoadedCallback;
1020-
script.src = scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' );
1021-
script.onerror = function() {
1022-
$.formUtils.warn('Unable to load form validation module '+scriptUrl);
1023-
};
1024-
script.onreadystatechange = function () {
1025-
// IE 7 fix
1026-
if (this.readyState === 'complete' || this.readyState === 'loaded') {
1027-
moduleLoadedCallback();
1028-
// Handle memory leak in IE
1029-
this.onload = null;
1030-
this.onreadystatechange = null;
1031-
}
1032-
};
1033-
appendToElement.appendChild(script);
1017+
if (typeof define === 'function' && define.amd) {
1018+
require([scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' )], moduleLoadedCallback);
1019+
} else {
1020+
// Load the script
1021+
script.type = 'text/javascript';
1022+
script.onload = moduleLoadedCallback;
1023+
script.src = scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' );
1024+
script.onerror = function() {
1025+
$.formUtils.warn('Unable to load form validation module '+scriptUrl);
1026+
};
1027+
script.onreadystatechange = function () {
1028+
// IE 7 fix
1029+
if (this.readyState === 'complete' || this.readyState === 'loaded') {
1030+
moduleLoadedCallback();
1031+
// Handle memory leak in IE
1032+
this.onload = null;
1033+
this.onreadystatechange = null;
1034+
}
1035+
};
1036+
appendToElement.appendChild(script);
1037+
}
10341038
}
10351039
}
10361040
});

src/main/module-loader.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,27 @@
8686
$.formUtils.loadedModules[scriptUrl] = 1;
8787
hasLoadedAnyModule = true;
8888

89-
// Load the script
90-
script.type = 'text/javascript';
91-
script.onload = moduleLoadedCallback;
92-
script.src = scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' );
93-
script.onerror = function() {
94-
$.formUtils.warn('Unable to load form validation module '+scriptUrl);
95-
};
96-
script.onreadystatechange = function () {
97-
// IE 7 fix
98-
if (this.readyState === 'complete' || this.readyState === 'loaded') {
99-
moduleLoadedCallback();
100-
// Handle memory leak in IE
101-
this.onload = null;
102-
this.onreadystatechange = null;
103-
}
104-
};
105-
appendToElement.appendChild(script);
89+
if (typeof define === 'function' && define.amd) {
90+
require([scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' )], moduleLoadedCallback);
91+
} else {
92+
// Load the script
93+
script.type = 'text/javascript';
94+
script.onload = moduleLoadedCallback;
95+
script.src = scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' );
96+
script.onerror = function() {
97+
$.formUtils.warn('Unable to load form validation module '+scriptUrl);
98+
};
99+
script.onreadystatechange = function () {
100+
// IE 7 fix
101+
if (this.readyState === 'complete' || this.readyState === 'loaded') {
102+
moduleLoadedCallback();
103+
// Handle memory leak in IE
104+
this.onload = null;
105+
this.onreadystatechange = null;
106+
}
107+
};
108+
appendToElement.appendChild(script);
109+
}
106110
}
107111
}
108112
});

0 commit comments

Comments
 (0)