shinsenter / defer.js

🥇 A lightweight JavaScript library that helps you lazy load (almost) anything. Defer.js is dependency-free, highly efficient, and optimized for Web Vitals.
https://shinsenter.github.io/defer.js/
MIT License
277 stars 45 forks source link

How to defer jquery librairy ? #93

Closed kressly closed 3 years ago

kressly commented 3 years ago

Anytime I defer Jquery it seems it does not load anymore. I am able to defer other codes that are under jquery libraries or that use jquery but not jquery itself. When I defer jquery like

<script type="deferjs" src="js/jquery.js"></script> Every other code that is under jquery stops working even though I have deferred them as well. Please how to defer jquery library ?

shinsenter commented 3 years ago

@kressly

Do you mean that the jquery.js file is not loading (case 1), or is it a problem that other code that calls jQuery gets exceptions (case 2)?

If your problem is the 2nd case then I think you have other scripts that call jQuery but haven't attached the defer attribute. I think it won't be a problem if you also defer other script tags that call jQuery. Otherwise, calls to jQuery will get exceptions because jQuery has not been declared when these scripts are executed by the browser.

shinsenter commented 3 years ago

@kressly

<script src="js/jquery.js" defer></script>

By the way, appending the defer attribute in your example is officially supported by most current browsers. That's not the way to use the library I introduced in the description of this library.

kressly commented 3 years ago

@kressly

<script src="js/jquery.js" defer></script>

By the way, appending the defer attribute in your example is officially supported by most current browsers. That's not the way to use the library I introduced in the description of this library.

It was a typing mistake when I was asking the question. I have corrected it with type="deferjs"

shinsenter commented 3 years ago

@kressly Have you tried defering other script tags?

kressly commented 3 years ago

@kressly Have you tried deferring other script tags?

Here are two examples, one where I did not defer jquery and one where I deferred jquery and every other library.

As soon as I defered Jquery and other librairies, the javascript slider stopped working and the bible audio player can not play anymore

shinsenter commented 3 years ago

@kressly I can see the issue. Maybe loading script with a crossorigin attribute may not work. I will fix it in the next version of the library.

shinsenter commented 3 years ago

@kressly Can you try copying this version of the library and check if it works?

kressly commented 3 years ago

@kressly Can you try copying this version of the library and check if it works?

I just did and I am still having the same problem.

kressly commented 3 years ago

@kressly Can you try copying this version of the library and check if it works?

I just did and I am still having the same problem.