Skip to content

Commit 1aa707d

Browse files
committed
Fix #182 - use document.createElement instead of document.write
This avoids warnings from Chrome, avoids possibility of being cancelled on slow connections. Page loads faster as the scripts are non-blocking. Ajax-selects initializes after all scripts are loaded.
1 parent 64afa5a commit 1aa707d

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

ajax_select/static/ajax_select/js/ajax_select.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
window.addEventListener('load', function() {
12

2-
(function($) {
3-
'use strict';
3+
var $ = window.jQuery;
44

55
$.fn.autocompleteselect = function(options) {
66
return this.each(function() {
@@ -245,4 +245,4 @@
245245
});
246246
});
247247

248-
})(window.jQuery);
248+
}, {once: true});
Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
1-
// load jquery and jquery-ui if needed
2-
// into window.jQuery
3-
if (typeof window.jQuery === 'undefined') {
4-
document.write('<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"><\/script><script type="text/javascript" src="//code.jquery.com/ui/1.10.3/jquery-ui.js"><\/script><link type="text/css" rel="stylesheet" href="//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />');
5-
} else if(typeof window.jQuery.ui === 'undefined' || typeof window.jQuery.ui.autocomplete === 'undefined') {
6-
document.write('<script type="text/javascript" src="//code.jquery.com/ui/1.10.3/jquery-ui.js"><\/script><link type="text/css" rel="stylesheet" href="//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />');
7-
}
1+
(function(w) {
2+
/**
3+
* load jquery and jquery-ui if needed
4+
*/
5+
6+
function not(thing) {
7+
return typeof thing === 'undefined';
8+
}
9+
10+
function loadJS(src) {
11+
var script = document.createElement('script');
12+
script.src = src;
13+
document.head.appendChild(script);
14+
}
15+
16+
function loadCSS(href) {
17+
var script = document.createElement('link');
18+
script.href = href;
19+
script.type = 'text/css';
20+
script.rel = 'stylesheet';
21+
document.head.appendChild(script);
22+
}
23+
24+
if (not(w.jQuery)) {
25+
loadJS('//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');
26+
}
27+
28+
if (not(w.jQuery) || not(w.jQuery.ui) || not(w.jQuery.ui.autocomplete)) {
29+
loadJS('//code.jquery.com/ui/1.10.3/jquery-ui.js');
30+
loadCSS('//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css');
31+
}
32+
})(window);

0 commit comments

Comments
 (0)