1

I have problem to return callable function written as php string.

On server side I have:

return = [
    'options' => [
        'timeout' => 3000,
        'callback' => [
            'close' => 'function(){
                alert("Test");
            }'
        ]
    ],
]

In my view model:

<?php echo '<script type="text/javascript">$.jQueryPlugin.defaults = '.json_encode($arrayFromAbove).'</script>';?>

But it does not work since callback is returned as string not callable js function.

Any advise how can I convert string as callable js function?

2
  • Please, paste the resulting JavaScript code you want to be written on the browser. Commented Mar 21, 2017 at 11:24
  • I think is a duplicate of stackoverflow.com/questions/359788/… Commented Mar 21, 2017 at 11:24

1 Answer 1

1

This is what JSON is for i.e. to standardize the data. So, if you have a string in php it will also treated as a string in JavaScript.

So, you need to avoid JSON for it. You just need to echo this separately without JS string quotes. Like:

$.jQueryPlugin.defaults.callback.close = <?php echo $arrayFromAbove['options']['callback']['close']; ?>;

This should work. Or, you can use eval(). But, use of eval is not recommended and is not a good practice to follow. See here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.