views:

88

answers:

3

Hi,

I use the JQuery dialog and from the PHP I can build some add-in to the button. To be able to add code from the server side I pass a method by parameter. The problem is that FireBug tell me that the method is not defined :

alt text

okHandler is the parameter of this method call to raise the dialog and it contain a simple alert message for the moment, later some Ajax calls. Any idea why it doesn't work?

alt text

+4  A: 

It looks like okHandler is a string containing a function declaration, not an actual function? You have

okHandler = "function anonymous(){alert('This is a test');}";

instead of

okHandler = function(){alert('This is a test');};
John Kugelman
+1  A: 

As John Kugelman notes, okHandler appears to be a string. It would work better if it was a function... However, if a string it must be, then you'll need to pass it through eval() to actually execute it:

eval( "(" + okHandler + ")()" )
Shog9
+1 it works with eval but in fact I was passing a String from the Server side to the client side. I gave you +1 but will accept John answer. Thanks Shog9
Daok
A: 

Is the okHandler() function loaded (as a valid JS object -- not a String) at the time you get that error?

I believe it's not alright to call something like "if (foo != null)" if foo hasn't already been declared as a variable somewhere. FireBug would complain: "okHandler is no defined."

Try something like this...

var myHandlers = {};
// Load okHandler as a member of myHandlers when applicable here...
$('#dialog'+idbox)...
    "Oky": function() {
        myHandlers.okHandler && myHandlers.okHandler();
        ...
    }
}
Drew Wills