I have a page which uses the long-polling technique to get data back from the server in parts, creating script tags containing IIFE expressions, that will look something like this:
<script>
(function(){
$("#blah").append("Some new data");
})();
</script>
If I get a LOT of these excess <script> tags, especially if the New Data is relatively large, I’m worried that it’s going to start slowing down the page.
Would there be any performance benefit removing the script tag from the DOM after it has executed?
<script id="thescript">
(function(){
$("#blah").append("Some new data");
})();
$("#thescript").remove();
</script>
EDIT: I feel like this question has become a trainwreck and the intention was unclear. Apologies. Really ultimately looking for the answer to a different question and this is one misguided step along the way.
You could instead just return JavaScript and use jQuery’s
$.getScript()method.Edit:
Maybe this would be a more direct answer.
If you request the script as plain text, you can then append it to the head and remove it.
I haven’t actually tried this, so I’m not 100% sure that it would work. It does sound logical though.
Another option would be to load the script into a particular element, then empty the element. It should have the same effect.
Edit Again: you could stick the script tags inside of a particular element with a class, then remove that element on success of the ajax call since you are returning more than just a script tag.