javascript在網頁頁面加載時的執行順序
javascript在頁面的執行順序
1、頁面上的javascript代碼是HTML文檔的一部分,所以javascript在頁面裝載時執行的順序就是其引入標記<[script /]>的出現順序, <[script /]>標記裡面的或者通過src引入的外部JS,都是按照其語句出現的順序執行,而且執行過程是文檔裝載的一部分。
3、變量的調用,必須是前面已經聲明,否則獲取的變量值是undefined。
5、document.write()會把輸出寫入到腳本文檔所在的位置,瀏覽器解析完documemt.write()所在文檔內容後,繼續解析document.write()輸出的內容,然後在繼續解析HTML文檔。
test.js的內容是:
var tmpStr = 1;
alert(tmpStr);
在Firefox和Opera中的彈出值的順序是:1、2、我是1、3
在IE中彈出值的順序是:2、1、3,同時瀏覽器報錯:tmpStr未定義
原因可能是IE在document.write時,並未等待加載SRC中的javascript代碼完畢後,才執行下一行,所以導致2先彈出,並且執行到 document.write(』document.write("我是" + tmpStr)』)調用tmpStr時,tmpStr並未定義,從而報錯。
解決這個問題,可以利用HTML解析是解析完一個HTML標籤,再執行下一個的原理,把代碼拆分來實現:
這樣IE下和其他瀏覽器輸出值的順序都是一直的了:1、2、我是1、3。
三、如何改變javascript在頁面的執行順序
1、利用onload
輸出值順序是 2、1。
需要注意的是,如果存在多個winodws.onload的話,只有最有一個生效,解決這個辦法是:
window.onload = function(){f();f1();f2();.....}
2009年1月16日 星期五
2008年11月14日 星期五
yaml,load_file
http://corelib.rubyonrails.org/classes/YAML.html
yml file
>>YAML.load_file("file_name")
>>{"defaults"=>{"sessions"=>false, "ttl"=>1800, "readonly"=>false, "fragments"=>true, "c_threshold"=>10000, "benchmarking"=>true, "namespace"=>"youholder", "debug"=>false, "servers"=>"localhost:11212", "urlencode"=>false, "compression"=>true}}
yml file
defaults:
ttl: 1800
readonly: false
urlencode: false
c_threshold: 10000
compression: true
debug: false
namespace: lalala
sessions: false
fragments: true
servers: localhost:11212
benchmarking: true
>>YAML.load_file("file_name")
>>{"defaults"=>{"sessions"=>false, "ttl"=>1800, "readonly"=>false, "fragments"=>true, "c_threshold"=>10000, "benchmarking"=>true, "namespace"=>"youholder", "debug"=>false, "servers"=>"localhost:11212", "urlencode"=>false, "compression"=>true}}
訂閱:
文章 (Atom)