16
16
[ ![ NPM] ( https://nodei.co/npm/@shinsenter/defer.js.png?downloads=true )] ( https://www.npmjs.com/package/@shinsenter/defer.js )
17
17
18
18
- ** Package** : [ @shinsenter/defer .js] ( https://www.npmjs.com/package/@shinsenter/defer.js )
19
- - ** Version** : 3.0 .0
19
+ - ** Version** : 3.1 .0
20
20
- ** Author
** : Mai Nhut Tan
< [email protected] >
21
21
- ** Copyright** : 2022 AppSeeds < https://code.shin.company/ >
22
22
- ** License** : [ MIT] ( https://code.shin.company/defer.js/blob/master/LICENSE )
@@ -80,7 +80,7 @@ Just put a `<script>` tag pointing to the library URL just below the opening `<h
80
80
<title >My Awesome Page</title >
81
81
82
82
<!-- Put defer.min.js here -->
83
- <script src =" https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.0 .0/dist/defer.min.js" ></script >
83
+ <script src =" https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.1 .0/dist/defer.min.js" ></script >
84
84
85
85
<!-- ... -->
86
86
</head >
@@ -96,7 +96,7 @@ Because `defer.min.js` is optimized to very tiny file size, you can even inline
96
96
<title >My Awesome Page</title >
97
97
98
98
<!-- Copy the script from below URL -->
99
- <!-- https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.0 .0/dist/defer.min.js -->
99
+ <!-- https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.1 .0/dist/defer.min.js -->
100
100
<script >/* then replace this comment block with the content of defer.min.js */ </script >
101
101
102
102
<!-- ... -->
@@ -114,7 +114,7 @@ just use `defer_plus.min.js` instead of `defer.min.js`.
114
114
<title >My Awesome Page</title >
115
115
116
116
<!-- Put defer_plus.min.js here -->
117
- <script src =" https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.0 .0/dist/defer_plus.min.js" ></script >
117
+ <script src =" https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.1 .0/dist/defer_plus.min.js" ></script >
118
118
119
119
<!-- ... -->
120
120
</head >
@@ -132,7 +132,7 @@ right after the `defer.min.js` script tag as following example:
132
132
133
133
<!-- If legacy browsers like Internet Explorer 9 still need to be supported -->
134
134
<!-- Please put IntersectionObserver polyfill right after defer.js script tag -->
135
- <script >' IntersectionObserver' in window || document .write (' <script src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.0 .0/dist/polyfill.min.js"><\/ script>' ); </script >
135
+ <script >' IntersectionObserver' in window || document .write (' <script src="https://cdn.jsdelivr.net/npm/@shinsenter/defer.js@3.1 .0/dist/polyfill.min.js"><\/ script>' ); </script >
136
136
```
137
137
138
138
* HINT* : Modern browsers support ` IntersectionObserver ` feature,
@@ -195,7 +195,7 @@ as soon as the page finished loading.
195
195
</script >
196
196
```
197
197
** Example**
198
- Sometimes, you would like your code not to run unless there is user activity.
198
+ Sometimes, you would like your code not to run unless there is a user activity.
199
199
200
200
The third argument tells the ` Defer() ` to delay the execution of the function
201
201
and wait until the user starts interacting with your page.
@@ -236,7 +236,7 @@ and wait until the user starts interacting with your page.
236
236
### Defer.lazy : <code >boolean</code >
237
237
The ` Defer.lazy ` variable was added since v3.0.
238
238
239
- Setting ` Defer.lazy = true ` tells the library to delay the execution
239
+ Setting ` Defer.lazy= true ` tells the library to delay the execution
240
240
of deferred scripts until the user starts interacting with the page
241
241
regardless of the page load event.
242
242
@@ -274,7 +274,8 @@ You can fully defer any script tag by setting its `type` attribute to `deferjs`.
274
274
This trick also works perfectly with ` <script> ` tags with a ` src ` attribute.
275
275
276
276
** Kind** : static method of [ <code >Defer</code >] ( #Defer )
277
- ** Note** : Lazy loading behavior changed since v3.0 when you set ` Defer.lazy=true ` .
277
+ ** Note** : Lazy loading behavior changed since v3.0
278
+ when you set ` Defer.lazy=true ` or ` waitForInteraction=true ` .
278
279
A ` <script> ` tags with ` type="deferjs" ` will not execute
279
280
unless the user starts interacting with your page.
280
281
** Since** : 2.0
@@ -325,6 +326,31 @@ If you hate using the `type="deferjs"` attribute, you can even choose your own o
325
326
Defer .all (' script[type="my-magic"]' , 5000 );
326
327
</script >
327
328
```
329
+ ** Example**
330
+ Using ` Defer.all() ` with script tags with ` src ` attribute:
331
+
332
+ Your scripts will work perfectly when you mix inline scripts
333
+ and script tags with a src attribute, like the below example.
334
+
335
+ The ` waitForInteraction ` argument (the fifth argument) is set to ` true ` ,
336
+ the library will defer the load of the tippy.js library until user starts
337
+ interacting, when user moves his/her mouse on the button, a tooltip wil show.
338
+
339
+
340
+ ``` html
341
+ <button id =" tooltip-button" >My button</button >
342
+
343
+ <script type =" myscript" src =" https://unpkg.com/@popperjs/core@2" ></script >
344
+ <script type =" myscript" src =" https://unpkg.com/tippy.js@6" ></script >
345
+
346
+ <script type =" myscript" >
347
+ tippy (' #tooltip-button' , { content: ' Hello from Defer.js!' });
348
+ </script >
349
+
350
+ <script >
351
+ Defer .all (' script[type="myscript"]' , 0 , true );
352
+ </script >
353
+ ```
328
354
329
355
* * *
330
356
@@ -652,7 +678,8 @@ We use the `Defer.css()` method to defer the load
652
678
of external CSS files without blocking the page rendering.
653
679
654
680
** Kind** : static method of [ <code >Defer</code >] ( #Defer )
655
- ** Note** : Lazy loading behavior changed since v3.0 when you set ` Defer.lazy=true ` or ` waitForInteraction=true ` .
681
+ ** Note** : Lazy loading behavior changed since v3.0
682
+ when you set ` Defer.lazy=true ` or ` waitForInteraction=true ` .
656
683
The ` fileUrl ` will not be fetched unless the user starts interacting with your page.
657
684
** Since** : 2.0
658
685
@@ -726,7 +753,12 @@ We use `Defer.js()` to defer the load of 3rd-party
726
753
javascript libraries, widgets, add-ons, etc. without blocking the page rendering.
727
754
728
755
** Kind** : static method of [ <code >Defer</code >] ( #Defer )
729
- ** Note** : Lazy loading behavior changed since v3.0 when you set ` Defer.lazy=true ` or ` waitForInteraction=true ` .
756
+ ** Note** : Because the download of file using ` Defer.js() ` function is asynchronous,
757
+ to avoid dependency error when lazy loading a third-party library using ` Defer.js() ` ,
758
+ it is highly recommended that the ` onload ` callback function be used
759
+ to make sure that the library you needed is completely defined.
760
+ ** Note** : Lazy loading behavior changed since v3.0
761
+ when you set ` Defer.lazy=true ` or ` waitForInteraction=true ` .
730
762
The ` fileUrl ` will not be fetched unless the user starts interacting with your page.
731
763
** Since** : 2.0
732
764
@@ -783,7 +815,8 @@ AddThis add-on will not be loaded until the user starts interacting with the pag
783
815
Lazy load Prism.js library.
784
816
785
817
Using Defer.js to lazy load Prism.js library and its assets.
786
- The ` <code> ` blocks on the page will be rendered only when you scroll to their positions.
818
+ The ` <code> ` blocks on the page will be rendered
819
+ only when the user scrolls to any ` code ` block position.
787
820
788
821
``` html
789
822
<style >
0 commit comments