views:

208

answers:

5
$('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!

A: 

what about

else{
    var lbjs  = document.createElement('script'),
        lbcss = document.createElement('link');


    lbjs.setAttribute('src', 'js/lightbox.js');
    lbjs.setAttribute('type', 'text/javascript');
    lbjs.onload = function(){
         $("a.lightbox").lightbox({
                'type': 'iframe',
                'overlayOpacity': 0.5,
                'width': 632,
                'hideOnContentClick': false
         });
    };

    lbcss.setAttribute('rel', 'stylesheet');
    lbcss.setAttribute('type', 'text/css');
    lbcss.setAttribute('href', 'css/lightbox.css');  

    document.getElementsByTagName('head')[0].appendChild(lbjs);
    document.getElementsByTagName('head')[0].appendChild(lbcss);
}
jAndy
+1  A: 
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();
}
BrunoLM
A: 

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.

Mark Schultheiss
fake inclusion is already achieved using $.ajax() I guess? The thing is I want to delay it until x secs after page load.
Nimbuz
so you could put the call in the page load with a timeoutsetTimeout("includeJS('js/lightbox.js')",10000);change the time out number to what you wish...
Mark Schultheiss
A: 

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);
Nimbuz
A: 

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.

CIDIC