一千萬個為什麽

搜索

JavaScript應該放在哪裏?

I've been doing a little JavaScript (well, more like jQuery) for a while now and one thing I've always been confused about is where I should put my scripts, in the <head> tag or in the <body> tag.

如果有人能澄清這個問題,那就太好了。應該去哪裏的一個例子是完美的。

最佳答案

Best practices from google and yahoo say that, for performance, JavaScript should always be put in an external file, and linked to your page with a <script> tag located at the bottom of the html, just before the closing of the <body> element.

這允許瀏覽器立即呈現整個頁面,而不是停止評估javascript。

轉載註明原文: JavaScript應該放在哪裏?