Javascript files appears duplicated in ajax navigation

问题: I´m having troubles with AJAX navigation, the problem is javascript files loaded remains in the browser after the new content is loaded even they arent in the DOM anymore,...

问题:

I´m having troubles with AJAX navigation, the problem is javascript files loaded remains in the browser after the new content is loaded even they arent in the DOM anymore, and they appears as VM files in the browser console and execute the code inside it, I dont want that happens because the javascript file it supposed to be replaced when the new content comes via AJAX.

My DOM structure is like this:

<body>
    <header></header>

    <main id="contentToBeReplaced">

        <p>New content with its own js</p>
        <script>
            var script = "js/new.js";
            $.getScript(script);
        </script>
    </main>

    <footer></footer>

    <script src="js/main.js"></script>
</body>

Every time a page is loaded with its own javascript file appears a new VM file and keeps all the olders, and thats a problem:
enter image description here

So, whats the problem and how can I fix this? I need to prevent duplicated files and remove the js file when a new its loaded.


回答1:

Add this at the begining of the "new.js" file to track that file in Chrome Dev Tools:

//# sourceURL=new.js

回答2:

Add 'use strict'; at the top of you JavaScript file it will enable a stricter mode of parsing JavaScript that prevents accidental globals. if it doesn't help you can read more about "garbage collector" in this article "4 Types of Memory Leaks in JavaScript and How to Get Rid Of Them"

  • 发表于 2019-07-08 00:28
  • 阅读 ( 285 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除