views:

47

answers:

1
// Lightbox
    $('a.lightbox').click(function () {
        $.getScript("js/lightbox.js", function () {
            alert('Load Complete');
            $("a.lightbox").lightbox({
                'type': 'iframe',
                'overlayOpacity': 0.6,
                'width': 940,
                'hideOnContentClick': false
            });
        });
    });

I want to load script on first request, but it doesn't seem to work, the page just redirects to the linked website, does not open iframe in lightbox.

Thanks for your help.

+4  A: 

You need to return false to prevent the event from propagating:

What is actually happening is that when the link is clicked, the code executes, but the event is finished, at which point the href attribute of the link (if it exists) is then redirected to.

(function($) {
    $(function() {
        $.getScript("js/lightbox.js", function () {
            alert('Load Complete');
            $('a.lightbox').click(function () {

               $("a.lightbox").lightbox({
                  'type': 'iframe',
                  'overlayOpacity': 0.6,
                  'width': 940,
                  'hideOnContentClick': false
               });
               return false;
            });  
        });
        $('a.lightbox').click();
   });
})(jQuery);
Jacob Relkin
Great, works, but the lightbox does not load in one attempt, can I add a manual delay just for the first request? thanks!
Nimbuz
@Nimbuz, updated my answer.
Jacob Relkin
Hmm..nothing happens, lightbox doesn't open.
Nimbuz
@Nimbuz, Try it now.
Jacob Relkin
Nope, still the same :(
Nimbuz
How about now??
Jacob Relkin
Erm...lightbox opens now but on second-click. BTW, can the jquery's $.delay() function be used here somehow? Thanks!
Nimbuz
No, `delay` only works with animation and CSS style chaining.
Jacob Relkin
That answer should do it.
Jacob Relkin