【发布时间】:2020-10-25 14:40:02
【问题描述】:
我试图在页面加载到我的 html(或 php)页面后动态包含一个 three.js 模块类型的脚本标记。这是我需要的,因为 google pagespeed 洞察力在一个有三个 js 的页面上严重失败,导致页面出现错误和糟糕的速度分数。
-
我已经尝试过 php readfile - 有效,但并没有真正改变速度测试的行为,我得到 LCP 的问号结果
-
我正在考虑在页面加载后尝试使用 xhr 插入脚本,这可能是一个解决方案吗?
settimeout 在这里不是一个选项
有什么想法吗?
【问题讨论】:
-
对,不要使用 settimeout,而是在 domcontentloaded 或 window on load 事件上执行 xhr,然后拉出该脚本
-
你现在如何加载three.js?你能提供一个minimal reproducible example你的代码吗?
-
@Dshiz 继承页面:带有普通脚本标签:adambernath.com/lotto/lotto.html 带有 php 读取文件:adambernath.com/lotto/lotto5.php
-
@KresimirPendic 你能给我一个例子来说明如何做到这一点吗?我已经尝试过,到目前为止我无法让它工作,但我可能做错了什么。到目前为止我已经做到了:
<script> function loadXMLDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("demo").innerHTML = this.responseText; } }; xhttp.open("GET", "haromd.js", true); xhttp.send(); }; loadXMLDoc(); </script> <script type="module" id="demo"> </script>
标签: javascript php xmlhttprequest