felixhagspiel / jsOnlyLightbox

Responsive Lightbox in plain JS. No need for jQuery.
MIT License
157 stars 51 forks source link

Allow deferring / loading lightbox.js asynchronously? #12

Closed curiositry closed 9 years ago

curiositry commented 9 years ago

When I defer the lightbox.min.js script, or load it asynchronously, it breaks.

Did I miss something, or is there any way to get around this? All the other scripts in my project are loaded async or deferred, and I’d prefer not to have a lightbox — which won’t be used until the content is ready — holding up the show.

Thanks!

curiositry commented 9 years ago

Is this project still active?

felixhagspiel commented 9 years ago

@omphalosskeptic sorry, havent seen your comment! I will continue developing as soon as I finish my other (paid ;) ) projects.

how do you init the lightbox, i.e. when? or how do you make sure atm that the lightbox is loaded before you init it?

curiositry commented 9 years ago

@felixhagspiel Thanks for your reply. Right now I’m doing it how you suggest in the readme (include the script, not deferred or async, then init in a script directly below).

felixhagspiel commented 9 years ago

can you send the console print? if you load the lightbox async then you have to wait until it is loaded before you init it. Try this and tell me if it works:

<script async src="js/lightbox.js" type="text/javascript"></script>
<script>
var interval = window.setInterval(function(){
  if(Lightbox) {
    var lightbox = new Lightbox();
    lightbox.load();
    window.clearInterval(interval);
  }
},50);
</script>
felixhagspiel commented 9 years ago

also, if you put the scripts at the bottom the DOM is ready when the lightbox is loaded. Except if you load the website content async. But then you should execute the init of the lightbox after the content is loaded, i.e. in the success-callback of your ajax call

curiositry commented 9 years ago

@felixhagspiel That seem to work. Thanks for responding to my poorly thought-out question!