views:

3546

answers:

4

Hello,

I have problem with fancybox.
I want to write a function that will run when the fancybox opened. How and when to call the function?

Example:

function myFunc() {
    alert("Opened!");
}

$('.content a').fancybox({ 
    'hideOnContentClick': false ,
    'callBackOnShow': myFunc(), // This not working! It call function when page is loaded
    'frameWidth': 920,
    'frameHeight': 530
});
+4  A: 

Instead of myFunc() use myFunc. In javascript, a function with parens (and/or args) means you want to call it. A function name without them means you just want a reference to that function (which is probably what fancybox wants from you)

Matt Briggs
+2  A: 

It should be:-

function myFunc() {
    alert("Opened!");
}

$('.content a').fancybox({ 
    'hideOnContentClick': false ,
    'callBackOnShow': myFunc, // no brackets
    'frameWidth': 920,
    'frameHeight': 530
});

Or, you can make an anonymous function...

$('.content a').fancybox({ 
    'hideOnContentClick': false ,
    'callBackOnShow': function() { alert('hello'); },
    'frameWidth': 920,
    'frameHeight': 530
});
rikh
+2  A: 

Actually, on checking the docs, you have a typo in your option list...

You have callBackOnShow but it should be callbackOnShow (lower case b)

rikh
+1  A: 
'callbackOnShow': myFunc

lower case b as the options are case sensitive, and no brackets on myFunc, as you want to pass the function as an option, not call it.

Mario Menger