简体   繁体   English

如何安全地加载外部 JavaScript 文件?

[英]How do you safely load external JavaScript file?

Is there a safe way to load external JavaScript file?有没有安全的方法来加载外部 JavaScript 文件? In a situation when the external or internal host/server/URL is NOT reachable?在无法访问外部或内部主机/服务器/URL 的情况下?

I want to know how can I make sure the page is not slowing down - it can look bad or miss functionality (due to the script not loading correctly).我想知道如何确保页面不会变慢 - 它可能看起来很糟糕或缺少功能(由于脚本加载不正确)。

Is "async=true" is all you need to do?您只需要“async=true”吗?

Thanks谢谢

You could use the async and/or defer attributes.您可以使用async和/或defer属性。

  • async , if the attribute is present, then the classic script will be fetched in parallel to parsing and evaluated as soon as it is available. async ,如果该属性存在,则经典脚本将与解析并行获取,并在可用时立即进行评估。
  • defer will indicate to a browser that the script is meant to be executed after the document has been parsed defer将向浏览器表明该脚本将在文档被解析后执行
<script 
    type="application/javascript" 
    src="https://example.com/something.min.js" 
    async 
    defer>
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM