0

I have a website that has a lot of popups, whenever a user load a popup (via ajax) there is a JavaScript code also loaded in the popup, I want this JavaScript code to be removed when its popup removed.

Main site code:

<body>

...

<div id="popup" style="display:none">

</div>
...

</body>

and then a loader in js file:

$.ajax('url',{
    success:function(data){
        $("#popup").show().html(data);
    };
});

one of the views that ajax call and push it in the popup:

 some html codes
<script>
$(document).on('click',function(){
console.log("document click");
});
</script>

when the user closes the popup, I use this code to empty the popup:

$("#popup").hide().html("");

but this doesn't end the on click event that was loaded, and any functions in the view will still exist in the memory and can be called!

this is an issue because I have a lot of popups here, and JavaScript eats the memory after a while, so I need to refresh the browser after using the website for some time, plus, events are duplicated whenever the popup reloaded!

If it not possible to clear these JS from the memory, are there any workaround for it?

Ryan Wilson
  • 7,490
  • 1
  • 17
  • 31
shamaseen
  • 1,221
  • 2
  • 11
  • 26
  • `$(document).off('click');` ?? - (https://api.jquery.com/off/), for the script which is loaded via `ajax` give it an `id` and use `.remove()` on the script element, but you'll still need to remove `event handlers` – Ryan Wilson Feb 11 '20 at 20:16
  • Does this answer your question? [Best way to remove an event handler in jQuery?](https://stackoverflow.com/questions/209029/best-way-to-remove-an-event-handler-in-jquery) along with this for removing a script (https://stackoverflow.com/questions/30072887/remove-a-script-with-jquery) – Ryan Wilson Feb 11 '20 at 20:18
  • @RyanWilson Thanks for the references, but no it doesn't answer the question. You see, the problem is not with events alone, it is with everything that comes along with the ajax request, I don't want functions to stay after the popup closed nor the events – shamaseen Feb 11 '20 at 22:41
  • AS I said, in the second link I gave you, give the `script` an `id` and remove it as well, thus the `functions` – Ryan Wilson Feb 12 '20 at 13:26
  • @RyanWilson Removing the script will not remove the functions, the functions will still out there in the memory and can be called! am I missing something? – shamaseen Feb 12 '20 at 17:07
  • 1
    I apparently misunderstood what you were trying to accomplish, the short answer is, there is no way to remove functions loaded by a dynamic script from memory without refreshing the page or causing a post back, but you can for lack of a better word `"destroy"` everything that was in your dynamic script by setting things to `undefined` and so forth, this would have to be done via a cleanup or unload function you called when removing the partial view, perhaps the accepted answer on this post (https://stackoverflow.com/questions/34997399/unload-a-javascript-from-memory) will get you going. – Ryan Wilson Feb 12 '20 at 18:53

0 Answers0