How can I check if a JS file has been included already?

前端 未结 5 2052
日久生厌
日久生厌 2020-12-15 18:24

I have split my javascript in different files. I want to prevent, that javascript files are loaded twice. How can I do that?

5条回答
  •  不思量自难忘°
    2020-12-15 19:13

    Not using any framework (that I know of), you can only do so by checking if a certain variable in the global scope has already been declared.

    You could declare a variable var include1 = true; inside include1.js, and do something like

    if (window.include1 !== true) {
        // load file dynamically
    }
    

提交回复
热议问题