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

4 Answers 4

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();
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

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

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);

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.