$('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:
- Start loading js/css on hover, but disable 'click' for x seconds until they're loaded.
- Onclick, delay the function for x seconds to open lightbox until the js/css are loaded.
- 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!
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.
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:
you can do similar with the CSS file. Note the ajax loading with jquery is well documented elsewhere on StackOverflow and the jQuery documentation.
Okay, this works for me: