-
Notifications
You must be signed in to change notification settings - Fork 13
/
example.html
240 lines (234 loc) · 11.7 KB
/
example.html
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>NBL.js 2.0 Examples</title>
<script src="nbl.js" data-nbl="['http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', 'http://www.google-analytics.com/urchin.js', function(){nbl_init_xmp1()}]"></script>
<script type="text/javascript">
// <!--
var nbl_count = 0, mtime = 0;
function nbl_init_xmp1() {
if (typeof jQuery == 'function') {
// nbl_xmp1 was called for the first time
$( '#result1' ).html( 'nbl_init_xmp1() was fired' ).addClass( 'pos' );
$( '#result1a' ).html( 'nbl.q.jquery is ' + nbl.q.jquery ).addClass( (nbl.q.jquery==true)?'pos':'neg' );
$( '#result1b' ).html( 'nbl.q.urchin is ' + nbl.q.urchin ).addClass( (nbl.q.urchin==true)?'pos':'neg' );
} else {
window.setTimeout( nbl_init_xmp1, 250 )
}
}
function nbl_init_xmp2() {
if (typeof $.fn.jcaption == 'function') {
// nbl_xmp1 was called for the second time
$( '#result2' ).html( 'nbl_inti_xmp2() was fired' ).addClass( 'pos' );
$( '#result2a' ).html( 'nbl.q.jcaption is ' + nbl.q.jcaption ).addClass( (nbl.q.jcaption)?'pos':'neg' );
$( "#photo-xmp2" ).jcaption({copyStyle: true, animate: true, show: {height: "show"}, hide: {height: "hide"}});
} else {
window.setTimeout( nbl_init_xmp2, 250 )
}
}
function clk_xmp2() {
nbl.l(['http://files.www.gethifi.com/posts/jcaption/jcaption.min.js', function(){nbl_init_xmp2()}]);
return false;
}
function clk_xmp3() {
mtime = (new Date()).getTime();
nbl.l([
3500, function(e){nbl_init_xmp3(e)},
'http://github.com/vitch/jquery-methods/raw/master/date.js', [ 'http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/scripts/jquery.datePicker.js', 'Non-existent URL' ]
]);
return false;
}
function nbl_init_xmp3(e) {
$( '#result3' ).html( 'nbl_init_xmp3() was fired after ' + ( (new Date()).getTime() - mtime ) + 'ms' ).addClass( 'pos' );
$( '#result3a' ).html( 'nbl.q.jquery is ' + nbl.q.jquery ).addClass( (nbl.q.jquery)?'pos':'neg' );
$( '#result3b' ).html( 'nbl.q.date is ' + nbl.q.date ).addClass( (nbl.q.date==true)?'pos':'neg' );
$( '#result3c' ).html( 'nbl.q.jquerydatePicker is ' + nbl.q.jquerydatePicker ).addClass( (nbl.q.jquerydatePicker==true)?'pos':'neg' );
$( '#result3d' ).html( 'nbl.q.NonexistentURL is ' + nbl.q.NonexistentURL ).addClass( (nbl.q.NonexistentURL==true)?'pos':'neg' );
$( '#result3e' ).html( (typeof e=='undefined')?'There were no errors':'There were errors' ).addClass( (typeof e=='undefined')?'pos':'neg' );
if ( nbl.q.jquerydatePicker == nbl.q.date ) {
$( '#datepickerlink' ).datePicker().dpSetPosition($.dpConst.POS_BOTTOM,0).dpSetOffset(0,92);
$( '#datepickerlink' ).removeClass( 'hidden' );
} else {
$( '#datepickerlink' ).html( 'There was an error' ).click( function(){return false} ).addClass( 'neg' ).removeClass( 'hidden' );
}
}
// -->
</script>
<style>
body { font-family: Georgia, serif; line-height: 1.5; }
#container { position: relative; margin: 1em auto 2em; padding: 4em; width: 48em; background: #def; border-radius: 0.5em; box-shadow: 0 0.5em 0.5em #888; -moz-box-shadow: 0 0.5em 0.5em #888; -webkit-box-shadow: 0 0.5em 0.5em #888; }
h1, h2 { font-family: Futura, "Century Gothic", AppleGothic, sans-serif; }
h1 { font-size: 400%; margin: 0; color: #678; text-shadow: 1px 1px 1px #fff; text-shadow: -1px -1px 1px #345; }
h2 { margin-top: 2em; }
h2.sub { margin: 0.5em; ;text-align: center; font-style: italic; font-weight: normal; color: #567; }
h3 em { font-weight: normal; }
hr { margin-bottom: 2em; height: 0; border: none; border-bottom: 1px solid #bcd; }
em { color: #bcd; }
.pos { color: green !important; }
.neg { color: red; }
.hidden { display: none; }
.center { margin-top: 0; text-align: center; }
/* For the image caption example */
.caption.none { position: relative; margin: auto; }
.caption.none p { position: absolute; margin: 0 auto; width: 100%; top: 0; right: 0; color: white; background-color: rgba( 255, 255, 255, 0.25 ); }
/* For the Date-picker example */
div.dp-popup { position: relative; background: #ccc; font-size: 10px; font-family: arial, sans-serif; padding: 2px; width: 171px; line-height: 1.2em; }
div#dp-popup { position: absolute; z-index: 199; }
div.dp-popup h2 { font-size: 12px; text-align: center; margin: 2px 0; padding: 0; }
a#dp-close { font-size: 11px; padding: 4px 0; text-align: center; display: block; }
a#dp-close:hover { text-decoration: underline; }
div.dp-popup a { color: #000; text-decoration: none; padding: 3px 2px 0; }
div.dp-popup div.dp-nav-prev { position: absolute; top: 2px; left: 4px; width: 100px; }
div.dp-popup div.dp-nav-prev a { float: left; }
/* Opera needs the rules to be this specific otherwise it doesn't change the cursor back to pointer after you have disabled and re-enabled a link */
div.dp-popup div.dp-nav-prev a, div.dp-popup div.dp-nav-next a { cursor: pointer; }
div.dp-popup div.dp-nav-prev a.disabled, div.dp-popup div.dp-nav-next a.disabled { cursor: default; }
div.dp-popup div.dp-nav-next { position: absolute; top: 2px; right: 4px; width: 100px; }
div.dp-popup div.dp-nav-next a { float: right; }
div.dp-popup a.disabled { cursor: default; color: #aaa; }
div.dp-popup td { cursor: pointer; }
div.dp-popup td.disabled { cursor: default; }
</style>
</head>
<body>
<div id="container">
<h1>NBL.js 2.0</h1>
<h2 class="sub">(a tiny non-blocking JavaScript lazy loader)</h2>
<a href="http://github.com/berklee/nbl/"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub" /></a>
<hr />
<p>
Here are some examples that demonstrate how to use NBL.js in a page.
</p
<!-- Example 1 -->
<div>
<h2>Example 1:</h2>
<p>Loading 2 (or more) scripts asynchronously:</p>
<pre><code>
<script src="nbl.min.js" data-nbl="[
'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js',
'http://www.google-analytics.com/urchin.js',
function(){nbl_init_xmp1()}
]"></script>
</code></pre>
<p>
By including this script tag in the head element of this document,
NBL.js will dynamically load jQuery from the first url and Urchin
from the second url in parallel.
</p>
<h3>Result: <em id="result1">loading…</em></h3>
<ul>
<li>jQuery loaded: <em id="result1a">loading…</em></li>
<li>Urchin loaded: <em id="result1b">loading…</em></li>
</ul>
<p>
The <code>nbl_init_xmp1()</code> function will fire when it has
completed loading the second script or when the default timeout has
been reached. If you change the position of the function in the
options like this:
</p>
<pre><code>
<script src="nbl.min.js" data-nbl="[
'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js',
function(){nbl_xmp1()},
'http://www.google-analytics.com/urchin.js'
]"></script>
</code></pre>
<p>
Then <code>nbl_init_xmp1()</code> will fire when it has
completed loading the first script. Basically, any function after
a script url will be fired when that script has loaded successfully.
You're free to declare a function after every script you load, but
keep in mind only the first function you declare will fire in case
of a timeout.
</p>
<hr />
</div>
<!-- Example 2 -->
<div>
<h2>Example 2:</h2>
<p>
NBL is now resident, and we can load a new script at any time.
Simply call:<br /> <code>nbl.l(['url', function(){nbl_init_xmp2()}])</code>
and a new script shall be loaded. After completion the <code>nbl_init_xmp2()</code>
function will fire.
</p>
<p class="center">
<a href="#" onclick="return clk_xmp2();">Click here</a> to add the jCaption jQuery plugin script to this page.
</p>
<h3>Result: <em id="result2">click first…</em></h3>
<ul>
<li>jCaption loaded: <em id="result2a">click first…</em></li>
</ul>
<p class="center">
<img id="photo-xmp2" alt="A view of Shinjuku, Tokyo." src="http://farm3.static.flickr.com/2787/4039125445_a2ce41cc69.jpg" />
</p>
<p>
After you've clicked the link, the photo should now have a nice caption
courtesy of the jCaption plugin.
</p>
<hr />
</div>
<!-- Example 3 -->
<div>
<h2>Example 3:</h2>
<p>
Let's find out what happens when you specify a wrong url for a script. We're going to call <code>nbl.l()</code> with some
new options, like this:
</p>
<pre><code>
nbl.l([ 3500, function(){nbl_init_xmp3()},
'http://github.com/vitch/jquery-methods/raw/master/date.js',
['http://www.kelvinluck.com/…/jquery.datePicker.js', 'Non-existent URL']
]);
</code></pre>
<p>
This will load a script with date extensions and two jQuery plugins, one date-picker and a non-existing one.
Trying to load the second plugin should cause an error, let's find out:
</p>
<p class="center">
<a href="#" onclick="return clk_xmp3();">Click here</a> to load the date picker scripts.
</p>
<h3>Result: <em id="result3">click first…</em></h3>
<ul>
<li><code>nbl_xmp3()</code> fired by jQuery: <em id="result3a">click first…</em></li>
<li>Date loaded: <em id="result3b">click first…</em></li>
<li>Plugin 1: jQuery Date Picker loaded: <em id="result3c">click first…</em></li>
<li>Plugin 2: Non-existent URL loaded: <em id="result3d">click first…</em></li>
<li>Errors: <em id="result3e">click first…</em></li>
</ul>
<p>
Alright, what happened here? First off, jQuery Date Picker and the Date script should load just fine. But Plugin 2
(the Non-existent URL) should fail to load immediately. This will cause NBL's timeout to go into effect, which we
specified to be 3500ms. So the above results should come into view 3.5 seconds after clicking. Plugin 2 will return
the script object with the faulty url still in its src attribute. Furthermore, the timeout function will be called
with the <code>nbl.q</code> object as its argument <code>e</code>, indicating there were errors.
</p>
<p>
I'll wait for Date and Plugin 1 to have loaded before showing you the date-picker link. The link should appear here:
<a id="datepickerlink" href="#" class="hidden"></a>
</p>
<hr />
</div>
<!-- Epilogue -->
<div>
<h2>That's it!</h2>
<p>
That should cover the basics of NBL.js, I hope I was able to explain its use. If you are unclear about something, feel free to
contact me: feedback at berkl.ee or @Berklee on Twitter.
</p>
<h3>Acknowledgements:</h3>
<p>
These examples use the following scripts:
</p>
<ul>
<li>John Resig's (and the jQuery team's) jQuery: <a href="http://www.jquery.com/">jQuery</a></li>
<li>Joel Sutherland's jCaption plugin: <a href="http://www.gethifi.com/blog/jcaption-a-jquery-plugin-for-simple-image-captions">jCaption</a></li>
<li>Kelvin Luck's jQuery Date picker plugin: <a href="http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/">Date picker v2</a></li>
<li>Jörn Zaefferer, Brandon Aaron and Kelvin Luck's Date methods: <a href="http://github.com/vitch/jquery-methods/">date.js</a></li>
<li>Google Analytics' Urchin script: <a href="http://www.google.com/analytics/">Google Analytics</a></li>
</ul>
</div>
</div>
</body>
</html>