Safari and Chrome doesn't evaluate dynamically added [removed] tag at page load

后端 未结 2 1075
清歌不尽
清歌不尽 2021-01-22 02:29

I am writing small JavaScript code which will load external js files at html page loading.

I tested 2 ways.

Case 1: Used document.write to add &

相关标签:
2条回答
  • 2021-01-22 02:52

    At the time when your javascript is evaluated, the DOM isn't fully built, the head element is not even finished. You normally cannot access or manipulate the DOM ( getElementsByTagName and appendChild, etc. ) before the HTML page is fully loaded.

    0 讨论(0)
  • 2021-01-22 03:04

    Try this instead:

    var s = document.createElement('SCRIPT');
    s.charset = 'UTF-8';
    s.src ='foo.js';
    document.getElementsByTagName('HEAD')[0].appendChild(s);
    

    But place this in a SCRIPT tag at the beginning of the BODY tag

    0 讨论(0)
提交回复
热议问题