-
Notifications
You must be signed in to change notification settings - Fork 24
/
jquery.ba-loadadscript.js
64 lines (50 loc) · 1.5 KB
/
jquery.ba-loadadscript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*!
* jQuery loadAdScript - v1.1 - 7/12/2010
* http://benalman.com/projects/jquery-misc-plugins/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function($){
'$:nomunge'; // Used by YUI compressor.
// A few references.
var doc = document,
write = doc.write,
writeln = doc.writeln,
// Create queue.
q = $.jqmq({
// Only iterate when q.next() is called.
delay: -1,
// For each queue item, do this.
callback: function( item ) {
// Override document.write and .writeln. Do we care that .writeln
// should append a newline character? Probably not.
doc.write = doc.writeln = function( html ) {
item.elems.append( html );
};
// Get script
$.getScript( item.url, function(){
// Execute callback if specified.
item.callback && item.callback.call( item.elems );
// Process next queue item, if exists.
q.next();
});
},
// When the queue completes, set document.write and .writeln back.
complete: function(){
doc.write = write;
doc.writeln = writeln;
}
});
// The plugin method.
$.fn.loadAdScript = function( url, callback ) {
// Add this to the queue.
q.add({
elems: this,
url: url,
callback: callback
});
return this;
};
})(jQuery);