简单优化script标签
introduce
雅虎35条军规(可能)里面有着这样一条,css放到head里面,js放到body的最下面,原因吗,应该是因为脚本会阻塞并行下载。。慌张, 我没仔细看。反正就是js要放在底部吗(强行解释)
随便扯扯
- 有些时候呢,我也不知道什么时候 (head里面会有一些莫名其妙的script标签) 可能是打包进去的, 也有可能是各种seo。(说的自己都心慌),那么就需要吧头部的script标签移到底部
Code
const scripts = document.head.getElementsByTagName('script');
const bodyFirstSrcipt = document.body.getElementsByTagName('script')[0];
for (const script of Array.from(scripts)) {
document.body.insertBefore(script, bodyFirstScript);
}
Writer&Contact
{
"name":"Jontyy" ,
"email": " jontyy@163.com",
"gitHub" : "https://github.com/YJD199798/"
}