1
我正在嘗試推遲JavaScript執行的Ryan Fioravanti的整潔技術。在他在Google I/O上發佈的slide 27和slide 28中對此進行了描述。在非WebKit瀏覽器中推遲JavaScript執行
在WebKit瀏覽器中,它的工作效果很好,與僅將script
標籤放在頁面底部或使用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>
所以腳本的類型被改變,但腳本本身沒有下載?或者它被下載並且不被執行?可能它只適用於webkit,你有沒有試過IE,Opera等? – 2012-07-11 07:13:08
@AndyDavies看起來像你釘了它。僅限Webkit。對我沒有測試更多瀏覽器感到羞恥。啊。我會更新這個問題。 – Trott 2012-07-11 18:15:44
您是否嘗試在將類型更改爲已知類型後重新設置該腳本的內容? – Bergi 2012-07-11 19:27:15