Onload event and js defer settings, onloadjsdefer

Source: Internet
Author: User

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);}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.