Onload event and js defer settings, onloadjsdefer
The onload event is executed after all nodes in the html document are downloaded, including js, css, and image resources.
If defer is set in js, The js Parsing is executed after the html document is generated in the browser, excluding the download of image resources.
If no defer is set for js, loading a js file in the head will block loading of the subsequent content.
Example
If defer = true is removed, it will not be executed until x1.jscript and x2.jscript are downloaded.
Alert ('win onload'); it will be executed only after the image is loaded.
If js and image resources are the same host, js loading will block image loading. images will be downloaded only after js download is complete.
Five parallel downloads are available in firefox.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">. Jscript is implemented through a java servlet to simulate a javascript file that requires a long time to download.
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 is also implemented through a java servlet, simulating an image download that requires a long time
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);}}