Okay I am working with some jquery creating dynamic popups. However, periodically the response will be the name of a function that I need to call instead of actual data and I am trying to figure out how to do this..
success: function(response) {
var obj = $.parseJSON(response);
if (obj.response == 'false') {
$('#popuperrDisplay').html(obj.msg);
$('#popuperrors').show();
} else {
if (obj.response == 'redirect') {
window.location.href = obj.msg;
} else if (obj.response == 'function') {
call obj
} else {
$(this).dialog('close');
}
}
}
This is the current portion of my jquery. I need to figure out how I can send the name of a JS function through json_encode and have JS actually call that function
If the function is defined globally (on the window object), you could simply invoke it like so:
Edit: (
window[]syntax)To further clarify,
window['showPopup']accesses theshowPopupmember of thewindowobject. It’s the same as doingwindow.showPopup. Only, the[]syntax has the advantage of receiving astringinstead of actual code.window['showPopup']will return an object (if defined); let it be a function for our particular example.We then call that function object by using the
()operator, like so:window['showPopup'](). Once again, this is the same as doing this:window.showPopup().Edit 2: (invoke with arguments)
If the function has to be invoked with an array of arguments, we can use apply. Invoking the function would then look like:
Where
arrayOfArgsis an array containing the arguments.