How to prevent $ .getScript from loading from the same file?

I am trying to load a .js file dynamically using $ .getScript. I have several choices for loading a different script for each click. I just want to call it once and, if possible, remove it from dom when I click another option, but firebug says it loads the same script again and again on the next subsequent clicks. After trying a few solutions, but finding unexpected results:

  • placing a callback or event in a callback. This person kills the click event on the next click. I still want the click to occur. Perhaps I did it wrong.
  • using .length to check if a url is needed.
  • using variables / function in script if it is already in dom already

Am I missing something, or should it be like that? I found the eternal question on this issue only in the jQuery forum :) I hope that it is more fortunate here. Thanks for the kind help.

+4
source share
1 answer

This is equivalent to getScript behind the scenes. Setting the cache parameter to true should fix your problem.

 $.ajax({ url: url, dataType: 'script', success: success, cache: true }); 
+7
source

Source: https://habr.com/ru/post/1300781/


All Articles