Home >Web Front-end >JS Tutorial >Research report on Javascript loading execution optimization_javascript skills
I have been doing front-end development for more than a year, and I still have some insights into the front-end. Today I will share it with you.
Front-end development requires various tools. For example, I am still accustomed to using Google browser and heavy weapon Fiddler.
1: Original situation
First of all, take a look at the following code:
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml">
It is estimated that 90% of programmers will put js files in the head, but have you ever looked into it? Many browsers will use a single thread to do "UI update" and "JS script processing",
That is, when the execution engine encounters "<script>", the downloading and rendering of the page must wait for the execution of <script> to be completed. Then it’s sad for users, looking at the locked page, </p>
<p>At this point the user is likely to turn it off for you. </p>
<p><img alt="" src="http://files.jb51.net/file_images/article/201412/201412160855464.png"></p>
<p>We can see two points from the waterfall chart above: </p>
<p>First place: </p>
<p>Three js files are downloaded in parallel, but according to my theory above, the js files should be executed one after another. However, in IE8, Firefox3.5 and Chrome2, parallel downloading of js is implemented, </p>
<p>This is pretty good, but it will still hinder the downloading of some other resources, such as pictures. </p>
<p>Second: </p>
<p>The download of picture 1.jpg is triggered after the js execution is completed, which also verifies the above situation and prevents the loading of the image. </p>
<p><strong>Two: First Step Optimization</strong></p>
<p>Since js prevents UI rendering, we can consider placing js before </body>, so that the html before <script> can be perfectly rendered and the user will not see the page blank waiting</p>
<p>The distressed situation naturally improves the friendliness. </p>
<p></p>
<div class="codetitle">
<span><a style="CURSOR: pointer" data="86272" class="copybut" id="copybut86272" onclick="doCopy('code86272')"><u>Copy code</u></a></span> The code is as follows:</div>
<div class="codebody" id="code86272">
<br>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="JsLoad.Default" %>
<p><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>"><br>
<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>"><br>
<head id="head"><br>
<title></title><br>
<link href="Styles/Site.css" rel="stylesheet" type="text/css" /><br>
</head><br>
<body><br>
<img src="1.jpg" width="200" height="300" /><br>
<script src="jquery/jquery-1.4.1.js" type="text/javascript"></script>