HTML5 optimization with low signal-to-noise ratio and HTML5 optimization with low signal-to-noise ratio

Source: Internet
Author: User

HTML5 optimization with low signal-to-noise ratio and HTML5 optimization with low signal-to-noise ratio

Baidu search engine suggests that our HTML files should not exceed KB. In fact, it is easy for large file search engines to crawl them, however, we try to streamline our webpage code as much as possible. We need to know that when a search engine crawls a webpage, it may not index the entire file, the index is only the previous part of the information. If the webpage code is too redundant, it is easy to push the part of our webpage article back, which is not good for the search engine to capture the webpage, therefore, we need to simplify the webpage code.

1. How to accelerate loading of HTML pages?

  • Page streamlining: Remove unnecessary spaces and comments from html pages. Try to write scripts and css in external files.
  • You can use a third-party tool to accelerate the page.
  • Reducing the number of files referenced on the page reduces the number of HTTP connections (src = "")
  • It is best to merge many JavaScript and CSS files.
  • Reduce external domain name file references
  • Optimize the loading sequence of page elements. For example, first load the content initially displayed on the page and the JavaScript and CSS related to the page, put the image files that are not needed to be loaded to the backend, or reference the delayed loading js files.
  • Reduce the number of inline and JavaScript in the page
  • Do not nest table labels in table labels, but now div + css is basically used, and HTML5 is also available.
  • Check whether the page has a js error or a blank reference (check whether the page has a 502 error), and whether there is a duplicate loading of js files.

2.Why keep labels clean and tidy?

  Client optimization has received much attention recently, but some basic aspects have been ignored. If you carefully observe some pages (even those pages that should have been optimized in depth), it is easy to find a lot of redundant and inefficient structures in their labels. All these burdens add unnecessary burdens to pages that should have been as lightweight as possible. Let's take a look at the most common mistakes: Add html comments to the script tag.

3. JS sliding event implementation

  It is well implemented on PC pages and boundClickAndMouseover. However, on mobile devices, the coreTouchEvent. ProcessingTouchEvents can be traced to each finger that slides on the screen.

Define the event handler function of touchstart and bind the event: if (!! Self. touch) self. slider. addEventListener ('touchstart', self. events, false); // defines the touchstart event processing function start: function (event) {event. preventDefault (); // The default action that prevents touch events, that is, to block scrolling var touch = event. touches [0]; // The touches array object obtains all the touch values on the screen, and obtains the coordinates of the first touch. startPos = {x: touch. pageX, y: touch. pageY, time: + new Date}; // bind event this. slider. addEventListener ('touchmove ', this, false); this. slider. addEventListener ('touchend', this, false );},
After the touchstart event is triggered, an event object is generated. The event object includes the touch list. The first touch on the screen is obtained and the coordinates of pageX and pageY are recorded; at this time, the touchmove and touchend events are bound;

4. The for... in statement in javascript is used to traverse the attributes of an object.

  The for... in statement is used to traverse the attributes of an object (cyclic operation is performed on the attributes of an object ).

JavaScript for... in statements are used to perform cyclic operations on the attributes of arrays or objects. Each time the Code in the for... in loop is executed, an operation is performed on the elements or attributes of the array. Syntax: for (variable in object) {execute code here} "variable" is used to specify a variable. The specified variable can be an array element or an object attribute.

5. Understand how javascript parsing engines work http://www.nowamagic.net/librarys/veda before and after Code Execution

  Simply put, the JavaScript parsing engine is a program that can "read" JavaScript code and accurately give the code running result. For example, when you writeVar a = 1 + 1;For such a piece of code, the JavaScript engine is to understand (PARSE) Your code and change the value of a to 2. Anyone who has learned the compilation principles knows that for static languages (such as Java, C ++, and C ),Compiler(Compiler), corresponding to dynamic languages such as JavaScript is calledInterpreter(Interpreter ). The difference between the two is summarized in one sentence: the compiler compiles the source code into another code (such as the machine code or bytecode ), the interpreter directly parses and outputs the code running result. For example, the firebug console is a JavaScript interpreter. However, it is difficult to define whether the JavaScript engine is an interpreter or a compiler, because, for example, the JavaScript engine like Chrome actually aims to improve the running performance of JS, before running the command, we will first compile JS into a local machine code (native machine code), and then execute the machine code (this is much faster) no stranger. In my opinion, I do not need to overemphasize what the JavaScript parsing engine is and understand what it actually does.

The JavaScript engine is a program. The JavaScript code we write is also a program. How can we make the program understand the program? Therefore, you need to define rules. For exampleVar a = 1 + 1;It indicates that the Left var represents the declaration, and it declares the variable. + On the right indicates that the addition of 1 and 1 is required. The equal sign in the middle indicates that this is a value assignment statement. The last semicolon indicates that the statement is over. The above are rules. With such rules, the JavaScript engine can parse JavaScript code based on the standards. ECMAScript defines these rules. ECMAScript 262 defines a complete set of standards for the JavaScript language. These include: how is a number, how is a string, and so on.StandardThe JavaScript engine is implemented according to this set of documents.StandardBecause there are also some implementations that do not follow the standards, such as the JS engine of IE. This is why JavaScript has compatibility issues. As for why IE's JS engine is not implemented in accordance with standards, it is necessary to talk about the browser war. Simply put, ECMAScript defines the language standard, and the JavaScript engine implements it based on it. This is the relationship between the two. So,What is the relationship between the JavaScript parsing engine and the browser?Simply put, the JavaScript engine is part of a browser. Because browsers also need to do a lot of other things, such as parsing pages, rendering pages, Cookie management, and historical records. Therefore, JavaScript Engines are developed by browser developers. For example, Chakra of IE9, TraceMonkey of Firefox, V8 of Chrome, etc. As a result, different browsers use different JavaScript Engines. Therefore, we can only explain which JavaScript Engine we need to know in depth.

 6. High-Performance HTML

Avoid using Iframe. All DOM elements in the test are empty. For example, loading a large script or style block may take longer than loading some iframe elements, but from the benchmark test results, even if it is an empty iframe, its overhead is very expensive. Given the high availability of iframe, we should avoid using it as much as possible. Especially for mobile devices, iframe should be avoided when most of them still have limited CPU and memory.

Avoid empty link property: the empty link property refers to the src or href attribute of the img, link, script, and ifrrame elements, but the property is empty. For example, we have created an image and set the image address to be empty. We hope to modify it dynamically in the future. However, even if the image address is empty, the browser still requests an empty address based on the default rule.

Avoid deep node nesting: deep-level nested nodes usually require more memory usage during initialization and will be slower during node traversal, this is related to the mechanism by which the browser builds DOM documents. By parsing the HTML Parser of the browser, the browser stores the structure of the entire HTML document as a DOM tree structure. The deeper the nested hierarchy of the document node, the deeper the DOM tree hierarchy.

The most obvious way to reduce the size of HTML documents and increase the download speed is to reduce the file size, especially to compress JavaScript and CSS code embedded in HTML documents, which can greatly simplify the page size. In addition, the following methods can be used to reduce the size of HTML documents: delete blank spaces and comments without affecting the execution results of HTM documents; Avoid Table layout; Use HTML5.

Avoid script blocking loading: When the browser is parsing regular script tags, it needs to wait until the script is downloaded and then parsed and executed, while subsequent HTML code can only wait. To avoid blocking loading, you should put your step at the end of the document, such as inserting the script tag before the body end tag:

 

<script src="example.js" ></script> </body>

 

 

Explicitly set the width and height of the image. When the browser loads the HTML code of the page, it sometimes needs to locate the page layout before the image download is complete. If the size (width and height) of the image in HTML is not specified, or the size described in the Code is different from that of the actual image, the browser needs to "backtrack" the image and re-display it after the image is downloaded, which consumes additional time. Therefore, it is best to specify the size of each image on the page, whether it is the label in the page HTML or in CSS.

 

 

7.

 

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Related Article

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.