Ashan77 Ashan77 - 2 months ago 23
jQuery Question

How to execute server returned javascript on client

I make an ajax call to a server and the server returns javascript and jquery code such as

$('someclass').html('<form id='billform'>......</>');
$('#billform').submit();


How do I execute this on the client side?

Answer

You could put that code inside a script tag and append it to the body This procedure is described best here: http://stackoverflow.com/a/611016/4202031

However, it is not recommendet to simply execute some javascript which is loaded via ajax. I would recommend to work with events that would trigger this code which is on your page already.

$.get('/sumUrl', function(data) {
  switch(data.action) {
    case 'event1':
      //do sth.
      break
    case 'event2':
      // do sth. else
      break
  }
})