JavaScript學習之代碼解釋

來源:互聯網
上載者:User

給你這段代碼加了注釋,希望對你有協助。
<script type="text/javascript" charset="utf-8">
(function() {
var doc = document,//給document對象定義個別名,局部變數訪問速度比全域的快
wa = doc.createElement('script'),//建立一個<script>標籤
s = doc.getElementsByTagName('script')[0];//找到第一個<script>標籤,有可能就是它自己
wa.type = 'text/javascript';//給剛建立的<script>標籤添加 屬性 ,此時的標籤:<script type="text/javascript">
wa.charset = 'utf-8'; //給標籤設定charset屬性,<script>不需要這個吧?
wa.async = true; //不知道這是個啥,沒見過
wa.src = ('https:' == doc.location.protocol ? 'https://' : 'http://') + 'js.t.sinajs.cn/open/analytics/js/suda.js?version=20111118190824.js';
//給<script>標籤 添加 src屬性,URL, 如果當前文檔的協議是https就 https://否則http:// 然後 連結上js的路徑 js.t.sinajs.cn/open/analytics/js/suda.js?version=20111118190824.js
//最後 這個標籤就變成了
// <script type="text/javascript" charset="utf-8" async="true" src="http(s)://js.t.sinajs.cn/open/analytics/js/suda.js?version=20111118190824.js"></scr ipt> //這裡我手動加了個空格,不然 會與最後一個script結束標籤衝突,這點請注意
s.parentNode.insertBefore(wa, s);})();//最後將這個標籤加入到頁面中第一個<script>標籤的 後邊,
/*
整個函數包含在一個匿名函數中,避免與外界的變數名同名時發生衝突。
整個過程實現的是一個動態載入外部js檔案的過程,這樣做的好處是,瀏覽器會並行下載js檔案,加快頁面的載入速度。
其實在最後將 那幾個doc的引用 卸載調就更好了,
doc = null;
wa = null;
s = null;
*/
</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.