JavaScriptの実行を延期するためのRyanFioravantiの巧妙なテクニックを試しています。これは、Google I/Oでの彼のプレゼンテーションのスライド27とスライド28で説明されています。
script
WebKitブラウザーでは、それはうまく機能し、ページの下部にタグを配置したり、async
属性 を使用したりするよりも、かなり良い結果を提供します。
ただし、FirefoxまたはIEでは機能しません。Firebugでは、スクリプトがDOMに挿入されていることがわかりますが、Firebugでは、スクリプト自体が実行されていないこともわかります。
この問題は、のような外部スクリプトとのような<script src="..."></script>
インラインスクリプト の両方で発生します<script>//inline code goes here</script>
。
誰かがこのテクニックをWebKit以外のブラウザで動作させるようになりましたか?
問題を再現するための関連するソースコードは次のとおりです。
<script type="text/notJs">
alert('This alert will show in Chrome and Safari thanks to the DOM manipulation below! But it does not show in Firefox or IE.');
</script>
<script>
window.onload = function () {
var scripts = document.getElementsByTagName('script');
var scriptIndex = 0;
for (var i = 0, len = scripts.length; i < len; i++) {
var scriptEl = scripts[scriptIndex];
if (scriptEl.type === 'text/notJs') {
scriptEl.type = 'text/javascript';
scriptEl.parentNode.removeChild(scriptEl);
document.body.appendChild(scriptEl);
} else {
scriptIndex++;
}
}
};
</script>