onload事件和js的defer設定

來源:互聯網
上載者:User

標籤:web   js   onload   defer   並行   


onload事件在html文檔中所有的節點都下載完成後執行,包括js,css,圖片的資源完全下載後才執行。
如果js 設定了defer之後,js的解析執行在瀏覽器產生了html文檔後執行,不包括圖片的資源下載

如果js沒有設定defer,將一個js放在head載入,會阻塞後面的內容載入。
如下面的例子
如果將defer=true去掉,直到x1.jscript和x2.jscript下載完成後,後面的alert(‘inline script‘);才會執行。
alert(‘win onload‘);會在圖片載入完成後才會執行。

如果js和圖片的資源是同一個host,js的載入會阻塞圖片的載入,只有等js下載完成後才下載圖片。

在firefox中是5個並行下載。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>test defer and onload</title>        <script type="text/javascript" src="http://localhost:9080/temp/x1.jscript?delaySec=10" defer=true></script>        <script type="text/javascript" src="http://localhost:9080/temp/x2.jscript?delaySec=5" defer=true></script>        <script type=text/javascript charset=utf-8 >alert(‘inline script‘);            window.onload = function(){                alert(‘win onload‘);            }</script></head><body>        <img src="http://localhost:9080/temp/test.img?delaySec=10&x=1" alt="long loading">        <img src="http://localhost:9080/temp/test.img?delaySec=10&x=2" alt="long loading">        <img src="http://localhost:9080/temp/test.img?delaySec=10&x=3" alt="long loading">        <img src="http://localhost:9080/temp/test.img?delaySec=10&x=4" alt="long loading">        <img src="http://localhost:9080/temp/test.img?delaySec=10&x=5" alt="long loading"></body></html>
.jscript通過一個java servlet來實現,類比一個需要一個比較長時間的javascript檔案下載
public class JSProcessor extends HttpServlet {protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {try {System.out.println("begin processing");String delaySec = req.getParameter("delaySec");Thread.currentThread().sleep(Integer.parseInt(delaySec)*1000);PrintWriter writer = resp.getWriter();writer.write("alert(‘done,delay + " + delaySec + "seconds‘)");System.out.println("end processing");} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}}
.img也是通過一個java servlet來實現,類比一個需要一個比較長時間的圖片下載

public class ImageProcessor extends HttpServlet {protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {try {resp.setContentType("image/jpeg;charset=GB2312");System.out.println("begin processing");String delaySec = req.getParameter("delaySec");Thread.currentThread().sleep(Integer.parseInt(delaySec)*1000);String imageFile = getServletContext().getRealPath("/blog_logo.jpg");System.out.println(imageFile);InputStream imageIn = new FileInputStream(new File(imageFile));JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);BufferedImage image = decoder.decodeAsBufferedImage();JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(resp.getOutputStream());encoder.encode(image);imageIn.close();System.out.println("end processing");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}}

onload事件和js的defer設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.