【发布时间】:2022-04-11 21:36:16
【问题描述】:
我已使用 PageSpeed Insights 测试了我的网站,但出现以下错误:避免使用 document.write()。我的网站上有一个用于跟踪访问者的 javascript 代码:
<script type="text/javascript" async>
document.write('<img src=\"https://traficwebsite/button.php?u=user&ref='+escape(document.referrer)+'&page='+escape(location.href.replace(/#.+$/,''))+'&rez='+screen.width+'x'+screen.height+'\" alt=\"DespreTrafic\" border=\"0\" width=\"1\" height=\"1\" />');
</script>
如何在这段代码中替换 document.write。谢谢!
【问题讨论】:
-
无论您是否将脚本声明为异步,文档写入仍处于阻塞状态。使用它是不好的做法,chrome 和 firefox 在评估它时都会抛出错误/警告。你也没有给你的用户一个忙,因为你的代码执行起来会很慢,假设你让它工作
标签: javascript pagespeed document.write