vs document.getElementsByTagName('script')[0] .insertBefore等?

时间:2022-09-03 19:02:11

Is there any material difference between:

以下是否有任何实质性差异:

<script type="text/javascript" src="/script.js" async></script>

and

(function() {
  var s = document.createElement('script'); 
  s.type = 'text/javascript'; 
  s.async = true; 
  s.src = 'script.js'; 
  var x = document.getElementsByTagName('script')[0]; 
  x.parentNode.insertBefore(s, x)
;})();

?

1 个解决方案

#1


I can only quote MDN:

我只能引用MDN:

In older browsers that don't support the async attribute, parser-inserted scripts block the parser; script-inserted scripts execute asynchronously in IE and WebKit, but synchronously in Opera and pre-4.0 Firefox.

在不支持async属性的旧浏览器中,插入解析器的脚本会阻止解析器;脚本插入的脚本在IE和WebKit中异步执行,但在Opera和4.0之前的Firefox中同步执行。

So in other words, the second one will still evaluate the script asynchronously in older IE and WebKit browsers.

换句话说,第二个仍将在较旧的IE和WebKit浏览器中异步评估脚本。

#1


I can only quote MDN:

我只能引用MDN:

In older browsers that don't support the async attribute, parser-inserted scripts block the parser; script-inserted scripts execute asynchronously in IE and WebKit, but synchronously in Opera and pre-4.0 Firefox.

在不支持async属性的旧浏览器中,插入解析器的脚本会阻止解析器;脚本插入的脚本在IE和WebKit中异步执行,但在Opera和4.0之前的Firefox中同步执行。

So in other words, the second one will still evaluate the script asynchronously in older IE and WebKit browsers.

换句话说,第二个仍将在较旧的IE和WebKit浏览器中异步评估脚本。