0

I need to load a dynamic created javascript with PHP. Is there any good solution to load javascript creating by PHP after some ajax requests finished?

Thx in regards, i am searching for 2h in stackoverflow.

if i call .ajax function where html/js respone, jquery kills the js ;/

9
  • 1
    you can put the response in eval() Commented Sep 17, 2013 at 8:49
  • works this only for js generated or if this result is with html too? Commented Sep 17, 2013 at 8:58
  • You can generate an entire HTML block with <script>your JS here</script> and append it to your document, you can create actual JS and use eval() like @shadow said or you can just rethink your approach and create a system that receives data from PHP and acts upon it. Commented Sep 17, 2013 at 8:59
  • 1
    @N.B. - thoughts I'd just mention that <script>...</script> blocks do not generally execute when you add them to the document. I believe some libraries (Underscore?) do a bit of work to simulate this, by scanning for <script> tags and eval(...)ing them, but it doesn't happen automatically in any modern browser. Commented Sep 17, 2013 at 9:03
  • 1
    Do you want to receive a JSON response, and then make a separate request to get some JavaScript, or are you looking to receive JavaScript as a response, and execute it immediately? Commented Sep 17, 2013 at 9:17

1 Answer 1

1
jQuery.ajax({
    type: 'POST',
    dataType: 'html',
    url: $url,
    success: function(result)
      { 
         result // html

         jQuery.ajax({
     type: 'GET',
     dataType: 'script',
         url: $url,
         success: function(result)
            { 
            //the result is js and is availble in browser if you return 
            //a function you can execute this now

            }
          });

      }
});

Hope someone find his answer here :) gl & hf & nice day

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

1 Comment

You should consider accepting your own answer. By that people can see the question is answered, and perhaps find the answer useful.

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.