延迟解析javascript文件
我了解到延迟javascript文件会导致页面的平滑打开和更少的加载时间……使用延迟解析javascript文件,javascript,html,javascript-events,Javascript,Html,Javascript Events,我了解到延迟javascript文件会导致页面的平滑打开和更少的加载时间……使用 <script type="text/javascript" defer="defer"> alert(document.getElementById("p1").firstChild.nodeValue); </script> <script type="text/javascript" defer="defer"> alert(document.getElementById
<script type="text/javascript" defer="defer">
alert(document.getElementById("p1").firstChild.nodeValue);
</script>
<script type="text/javascript" defer="defer">
alert(document.getElementById("p1").firstChild.nodeValue);
</script>
<script type="text/javascript" defer="defer">
alert(document.getElementById("p1").firstChild.nodeValue);
</script>
警报(document.getElementById(“p1”).firstChild.nodeValue);
警报(document.getElementById(“p1”).firstChild.nodeValue);
警报(document.getElementById(“p1”).firstChild.nodeValue);
我指的是上面代码中的“延迟”,但我读到它只在IE中起作用……是否有其他方法延迟JS的解析……我已经压缩了它我应该在
窗口上附加一个事件。onload
在加载窗口时触发
例如:
function deferMethod() {
// you're code here
}
// Check for browser support of event handling capability
if (window.addEventListener)
window.addEventListener("load", deferMethod, false);
else if (window.attachEvent)
window.attachEvent("onload", deferMethod);
else
window.onload = deferMethod;
我应该在
窗口上附加一个事件。onload
在加载窗口时触发
例如:
function deferMethod() {
// you're code here
}
// Check for browser support of event handling capability
if (window.addEventListener)
window.addEventListener("load", deferMethod, false);
else if (window.attachEvent)
window.attachEvent("onload", deferMethod);
else
window.onload = deferMethod;
您只需删除2个脚本元素,并在第一个脚本元素中添加一个onload。在这个onload函数中,您可以将脚本元素写入dom,它们将异步加载。查看此链接:您可以删除2个脚本元素,然后在第一个脚本元素中添加一个onload。在这个onload函数中,您可以将脚本元素写入dom,它们将异步加载。查看此链接:您可以将不太重要的脚本移动到页面底部。您可以将不太重要的脚本移动到页面底部。