0

At my Ajax response there is two variable: js, html.

The js variable contents Javascript in tag script: //Some code And html contents HTML template. How I can execute js code for html template from the successful response Ajax?

I tried this solution:

$('head').append(response.js); // not works

The full Ajax request:

getFormShare: function(type){
        var posting = $.post('/share/getFormShare', { type : type } );

        posting.done(function( response ) {
            response = $.parseJSON(response);

            $("head").append(response.js);
            $('.ShareBlock #block').html(response.html).show();

            initFileUpload(config);
            initEditor();

        });
    }

The PHP side:

$js = $this->listdata->WrapperJavaScript($specialization, array('name_field' => 'type[]', 'tab' =>'tab', 'div_element' => '.specMain', 'label' => 'Category', 'autosearch' => 'true'));

$html = $this->load->view('social/forms/video', $this->data, true);
echo json_encode(array('html' => $html, 'js' => $js)); die();
1
  • What does the JSON data look like? I'm not a PHP programmer. Commented Sep 15, 2014 at 14:34

2 Answers 2

5

You can use .getScript() and run your code after it loads:

 $.getScript("my_lovely_script.js", function(){

    alert("Script loaded and executed.");
    // here you can use anything you defined in the loaded script

 });

You can see a better explanation here: How do I include a JavaScript file in another JavaScript file?


Edit: Since you're returning actual javascript and would like to execute it, you can simply do this in your ajax response:

 $.get(url,function(data){ $("body").append(data); });

Edit: And with help from Mike's answer, you can use eval() to run the script if you don't have script tags in your response. His answer gives more info on this.

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

11 Comments

Problem is that I have not a ready file js. It is generated by PHP and returned from Ajax. I need execute a some code js only.
The second your edit also is not good, I have a tag <script> in result response, also in question I told that u tried: $('head').append(response.js);
Is the script or the file generated by PHP, the OP says one thing and you're saying another.
At response there is code: <script>//SOME CODE</script>
Then my 2nd edit should work. And just to make sure you're not really using response.js as your variable, you can't use . in variable names.
|
1

You can utilize javascript's eval to run string as code.

Keep in mind your string needs to be pure JS, no html or other elements to it.

example

eval("function foo() {console.log('bar');}");
//this call will create/instantiate a function called foo

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.