Preload resource inside the browser
1.static tag
<link rel="preload" as="...">
Just load some resource, not be run.
<link rel-"preload" href="/pre/res/stylepre.css" as="style">
2. dynamic adding into head by script
<script>
const prelink = document.createElement('link');
prelink.rel="preload";
prelink.as="style";
prelink.href="/pre/res/stylepre.css";
document.head.appendChild(prelink);
</script>
3. check
const bPreload = ()=>{
const pre=document.createElement('link');
const plist=pre.relList;
if(!plist || !plist.supports){ return false;}
return plist.supports('preload');
}
4.onload
4.1 call back function
<link rel-"preload" href="..." as="..." onload="preloadDone()">
4.2 running all right
<link rel-"preload" href="/pre/res/stylepre.css" as="style" onload="this.rel=stylesheet">
5. crossorigin
<link rel-"preload" as="font" crossorigin href="https://pre.res/fontX.woff">
font resource must include tag "crossorigin".
<link rel="preload" as="...">
Just load some resource, not be run.
<link rel-"preload" href="/pre/res/stylepre.css" as="style">
2. dynamic adding into head by script
<script>
const prelink = document.createElement('link');
prelink.rel="preload";
prelink.as="style";
prelink.href="/pre/res/stylepre.css";
document.head.appendChild(prelink);
</script>
3. check
const bPreload = ()=>{
const pre=document.createElement('link');
const plist=pre.relList;
if(!plist || !plist.supports){ return false;}
return plist.supports('preload');
}
4.onload
4.1 call back function
<link rel-"preload" href="..." as="..." onload="preloadDone()">
4.2 running all right
<link rel-"preload" href="/pre/res/stylepre.css" as="style" onload="this.rel=stylesheet">
5. crossorigin
<link rel-"preload" as="font" crossorigin href="https://pre.res/fontX.woff">
font resource must include tag "crossorigin".
评论
发表评论