How to run/load a separate javascript file selectively from inside javascript?

前端 未结 4 445
生来不讨喜
生来不讨喜 2021-01-24 16:01

I have 3 JavaScript files that I would like to load from an index.html file. I\'m choosing among them by using if statements which I only know how to d

4条回答
  •  青春惊慌失措
    2021-01-24 16:38

    I'm new to javascript but i have an idea you can set id to just like

    then you can append your script to header by using this code:

    var h = document.getElementById('h');
    var scripts = document.createElement('script');
    switch(window.innerHeight){
        case 800:
          scripts.src = 'storybook.js';
          scripts.type = 'text/javascript';
          break;
    }
    h.appendChild(scripts);
    

    You can change scripts source by add more case. Thanks for reading.

提交回复
热议问题