Lazy loading a plugin (jQuery)

2019-05-07 03:33发布

问题:

$('a.lightbox').hover(function () {
    if (jQuery().lightbox) {
        // required, otherwise lightbox.js will be loaded on hover each time
        $("a.lightbox").lightbox({
            'type': 'iframe',
            'overlayOpacity': 0.5,
            'width': 632,
            'hideOnContentClick': false
        });
    } else {
        // load script
        $.ajax({
            url: "js/lightbox.js",
            dataType: 'script',
            cache: true,
            success: function () {
                // load css
                $('head').append('<link rel="stylesheet" type="text/css" href="css/lightbox.css">');
                // lightbox function
                $("a.lightbox").lightbox({
                    'type': 'iframe',
                    'overlayOpacity': 0.5,
                    'width': 632,
                    'hideOnContentClick': false
                });
            }
        });
    }
});

... this works perfectly on local machine, but not quite when uploaded to server because the 12kb lightbox.js and the lightbox.css takes some time to load.

I would like to do either of these:

  1. Start loading js/css on hover, but disable 'click' for x seconds until they're loaded.
  2. Onclick, delay the function for x seconds to open lightbox until the js/css are loaded.
  3. Delay loading of lightbox.js and lightbox.css for about 1 min after the page has loaded.

I prefer the 3rd option, but have no idea how to implement any of these.

I'd appreciate any help! Thanks!

回答1:

success: function () {
    // load css
    $('head').append('<link rel="stylesheet" type="text/css" href="css/lightbox.css">');

    WaitLightbox(function () {/*lightbox funcion*/});
}


function WaitLightbox(callback)
{
    if (jQuery().lightbox === undefined)
        setTimeout(function(){WaitLightbox(callback);}, 100);
    else
        callback();
}


回答2:

You can use the jQuery portion to load after the page loads with a function or ajax with no timers, just loads after the page is ready.

Example:

/* function to allow inclusion of other files 
Included this way to allow addition AFTER the page loads- that is, fake inclusion in this Javascript file
*/
function includeJS(jsPath)
{
    var script = document.createElement("script");
    script.setAttribute("type", "text/javascript");
    script.setAttribute("src", jsPath);
    document.getElementsByTagName("head")[0].appendChild(script);
};
$(function()
{
includeJS('js/lightbox.js');
});

you can do similar with the CSS file. Note the ajax loading with jquery is well documented elsewhere on StackOverflow and the jQuery documentation.



回答3:

Okay, this works for me:

setTimeout(function(){
    $.ajax({
        url: "js/lightbox.js",
        dataType: 'script',
        cache: true,
        success: function () {
            $('head').append('<link rel="stylesheet" type="text/css" href="css/lightbox.css">');
            $("a.lightbox").lightbox({
                'type': 'iframe',
                'overlayOpacity': 0.5,
                'width': 632,
                'transitionIn': 'elastic',
                'transitionOut': 'elastic',
                'hideOnContentClick': false
            });
        }
    });
}, 10000);


回答4:

I came across something similar to this here http://www.1stwebdesigner.com/development/quick-tip-get-more-efficient-with-jquerys-getscript/ I don't know if that helps.