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();
Alice
  • 144
  • 11

2 Answers2

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.

Community
  • 1
  • 1
Control Freak
  • 12,965
  • 30
  • 94
  • 145
  • 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. – Alice Sep 15 '14 at 14:08
  • The second your edit also is not good, I have a tag ** – Alice Sep 15 '14 at 14:11
  • Is the script or the file generated by PHP, the OP says one thing and you're saying another. – Control Freak Sep 15 '14 at 14:14
  • At response there is code: `` – Alice Sep 15 '14 at 14:18
  • 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. – Control Freak Sep 15 '14 at 14:19
  • Is there really a good reason to append the "script" tags into the HTML? You're bloating the DOM and you can already get the Javascript to be loaded into the page with eval. Seems like an exercise in superfluity... – Mike Sep 15 '14 at 14:21
  • @Alice You should post your ajax code, otherwise there's no way to really help. – Control Freak Sep 15 '14 at 14:22
  • @Mike thats how I do it, the original question seemed to ask otherwise. I am unfamiliar with other methods, since this works fine. – Control Freak Sep 15 '14 at 14:22
  • Unless this is some page that gets saved offline, I don't see a good reason to append scripts into the DOM. – Mike Sep 15 '14 at 14:22
  • @Mike true, and I just +1 your answer because I will now use that. :) – Control Freak Sep 15 '14 at 14:23
  • @ZeeTee np mate, I've +1'd you because you at least are attempting to do what the question asked for, even if it might not be the best or most efficient method. She hasn't really given us much information about what she is trying to do, and I'm not sure why you would need to load scripts with AJAX unless these scripts are being generated by a server. – Mike Sep 15 '14 at 14:25
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
Mike
  • 874
  • 1
  • 8
  • 15