In the process of trying to mimic sites like Youtube, and loading only the specific fragments of a page on link click, I have tried making a function that loads/unloads the specific JS files and CSS files needed for the current page.
I've noticed that when CSS files get removed from the DOM, the styling from that file is no longer vaild, and only gets valid again if I add the specific link tag again. However, it seems like JS files get stored in the memory when loaded, and simply removing the reference in the DOM doesn't prevent the functions associated with that file to still work. How does Youtube go around this problem, or do you have your own suggestions on which path I should go to achieve unloading/loading JS files "completely"?
One idea I had was to just have all variables, element IDs etc in my code (both when it comes to HTML and JS) to be unique, and have an array that keeps check of which JS files have been loaded - so that there won't be more than one of each JS file in memory. But I guess that's not a safe solution, since the GC can remove a JS file from memory if that "page" hasn't been used for long?