user892134
user892134

Reputation: 3214

How to reload js script after ajax call?

I have this script called admin_order.js, which works fine. Due to a certain plugin which loads ajax content after ajax content has been loaded admin_order.js now doesn't work.

I tried this in the response function but no success.

$.getScript("../../themes/mywebsite/js/admin_order.js");

How do i reload the script?

Upvotes: 2

Views: 12846

Answers (1)

Irvin Dominin
Irvin Dominin

Reputation: 30993

I think the problem must be handled in another way; but if you can't handle this in the plugin code, you can:

  1. add an id to the script element eg myAdminOrderScript
  2. before the getScript remove the script element
  3. in the getScript success function add an Id to the last script element

Like:

<script id="myAdminOrderScript" src="../../themes/mywebsite/js/admin_order.js"></script>

  function reloadScript() {
    $('#myAdminOrderScript').remove();
    $.getScript("../../themes/mywebsite/js/admin_order.js", function() {
      $('script:last').attr('id', 'myAdminOrderScript');
    });
  }

Upvotes: 2

Related Questions