OnLoad event and JS defer settings

Source: Internet
Author: User


The OnLoad event executes after all the nodes in the HTML document have been downloaded, including Js,css, and the picture's resources are fully downloaded.
If JS set the defer, JS parsing execution after the browser generated HTML document execution, not including the image of the resource download

If JS is not set defer, put a JS in the head load, will block the subsequent content loading.
As the following example
If Defer=true is removed, the following alert (' inline script ') will not execute until X1.jscript and X2.jscript are downloaded.
Alert (' win onload '); will not execute until the picture is loaded.

If JS and the image of the resource is the same Host,js loading will block the loading of the picture, only after the JS download is complete before downloading the picture.

In Firefox, there are 5 concurrent downloads.

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >. JScript is implemented by a Java servlet, simulating a download of a JavaScript file that requires a relatively long time
public class Jsprocessor extends HttpServlet {protected void doget (HttpServletRequest req, HttpServletResponse resp) thro WS 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 by a Java servlet, simulating a picture that takes a long time to download

public class Imageprocessor extends HttpServlet {protected void doget (HttpServletRequest req, HttpServletResponse resp) t Hrows 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 event and JS defer settings

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.