动态创建脚本标记和静态嵌入脚本标记有什么区别? [英] What is the difference between dynamically creating a script tag and statically embed a script tag?

查看:211
本文介绍了动态创建脚本标记和静态嵌入脚本标记有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道很多网络分析工具使用以下脚本动态创建脚本标记:

I know lots of web analytic tools use the following script to create a script tag dyanmically:

<script type="text/javascript">
    var s = document.createElement( 'script' );
    s.type = 'text/javascript';
    s.src = 'http://s/s.js';
    document.body.appendChild( s );</script>

我的问题是,如果我静态嵌入脚本,如下所示:

My Question is if there is a difference if I embed the script statically like the following:

<script type="text/javascript" src="http://s/s.js"></script>


推荐答案

他们这样做是为了让它以异步方式加载,或者在至少所以只有在正常的JS加载并执行后才会加载它。它避免了渲染树等待加载这个JS,因此页面更快。

They do it so that it loads asynchronously, or at least so it is only loaded once the normal JS has loaded and been executed. It avoids making the rendering tree wait for this JS to load so the page is quicker.

这篇关于动态创建脚本标记和静态嵌入脚本标记有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆