JS allows us to modify all aspects of the page: the content, the style, and the behavior of the user when interacting.
But JS can also block the formation of the DOM tree and delay the rendering of the page.
Let your JS become asynchronous execution, and reduce unnecessary JS files to improve performance.
- JavaScript can query and modify DOM and Cssom
- JavaScript execution blocks the execution of Cssom
- JavaScript blocks the formation of the DOM unless the special declaration JS executes asynchronously
JS is a synchronous language that can modify any aspect of a webpage:
<HTML> <Head> <Metaname= "Viewport"content= "Width=device-width,initial-scale=1"> <Linkhref= "Style.css"rel= "stylesheet"> <title>Critical Path:script</title> </Head> <Body> <P>Hello<span>Web Performance</span>students!</P> <Div><imgsrc= "Awesome-photo.jpg"></Div> <Script> varspan=document.getElementsByTagName ('span')[0]; Span.textcontent= 'Interactive'; //Change DOM text contentSpan.style.display= 'inline'; //Change CSSOM Property //Create a new element, style it, and append it to the DOM varLoadtime=Document.createelement ('Div'); Loadtime.textcontent= 'You loaded the page on:' + NewDate (); LoadTime.style.color= 'Blue'; Document.body.appendChild (Loadtime); </Script> </Body></HTML>
-
JS allows us to manipulate the DOM and use a reference to the hidden object--the node may not be visible in the render tree, but it still exists in the DOM! So, we can use a reference to the hidden object to change the text (via the Textconten property), and even rewrite the display property that has been computed from the original ' none ' to ' inline '. Once these are completed, our page will be displayed as "Hello interactive students!"
-
JS can also be created from the DOM, add and remove new elements, or set styles for elements. In fact, technically, our page can be created with a huge JS file to create elements and set the style for the elements. But the actual operation, HTML and CSS more convenient. In the function in the second part of the example we created a new DIV element and set the text element, styled it, and appended it to the BODY element.
But we're not going to be able to do this before the DOM is done. dom--the browser will execute JS and then execute the DOM tree when it encounters JS--so when the built-in JS file will block the formation of the DOM, it can also delay the initialization of the rendering.
JS can also operate Cssom, if the browser does not end the download and Cssom construction, we would like to run JS it? The answer is that the browser will run aground until the Cssom is loaded and constructed, and when we wait for the execution, the DOM constructs are also blocked.
In short, JS introduces a series of features are dependent on DOM and Cssom, JS execution can also lead to browser execution and page rendering a huge delay:
- The location of the script in the page is very important
- When a script tag is encountered, the DOM construction process stops until the end of the script execution
- JS can query and manipulate DOM and Cssom
- The execution of JS will be delayed until Cssom is ready.
Parsing blocking vs. asynchronous JS
We can include the async attribute in the script tag to tell the browser to execute the DOM construct while executing the JS script.
Original: https://developers.google.com/web/fundamentals/performance/critical-rendering-path/ adding-interactivity-with-javascript?hl=en
Execute JS script asynchronously--prevent blocking