I learn a lesson that putting javascript codes/files into last part of body area in normal situations because javascript codes can actually access DOM in body tag area. If I put them in header area of html file, it won’t work as I expect. According to my friends' explanations, it runs first before the body renders complete in normal scenarios. I learn the lesson when I was trying to generate a chart through D3js.
先前在練習 D3js 時,發現我若依 Rails 的慣例將 Javascript 的程式碼或檔案放在 header 區域,我的 D3js 程式碼是沒有任何效果的,不如預期地執行。強者我朋友們解答說:一般情況下,如果將 javascript code 放在 header 區域,它將在 body render 完畢之前,就開始執行,可是因為 DOM 還沒執行完畢,所以 D3js 程式碼就選不到 DOM。