Website code optimization is a basic skill that webmasters must master, which is related to whether search engine spiders will be interested in websites. Lengthy and useless code will make it difficult for spiders to understand and increase the difficulty for spiders to crawl websites. At the same time, the simplification of web pages is also related to the loading speed of the website, which is very important for the user experience. Website code optimization is mainly optimized from the following aspects.
1, standardization of some codes of magnetic head.
2. Layout the web page with DIV+CSS.
Some websites will use external files and put css and js in external files. Just put a code call in the html of the page. Sometimes when we look at some source code, we will see a lot of css code and javasript code, and put javascript in front of the html file of the website page, but some really usable text parts are pushed behind the html and need to be streamlined.
3.CSS optimization
CSS is a very important part of page effect presentation, including color, size, background and font. Writing CSS is easy, but there are still many skills to write refined CSS code.
(1), CSS position
Css shows that if it appears behind the website, the page needs to be re-rendered, and the opening speed will be affected. All CSS definition codes should be placed in front of the website.
(2) css sprite technology
Some pictures on the website can be merged by css sprite technology, which reduces the number of loading requests and improves the loading speed of web pages.
(3) CSS code optimization
By shortening css code attributes, removing redundant structures, resetting and a series of methods and technologies, css codes are simplified and the size of css files is reduced.
(4) Try not to use embedded CSS.
There are two kinds of embedded CSS, one is the common embedded CSS in the head area; The second is the embedded CSS that appears in the tag. No matter how CSS is embedded, the page size will increase. In this regard, we can try to use external CSS to reduce the size of website pages.
4.JS optimization
JS optimization and optimization of other languages still have something in common. The key to JS optimization is to focus on the most critical place, which is the bottleneck. Generally speaking, bottlenecks always appear in places with large-scale circulation. This is not to say that the loop itself has performance problems, but that the loop will quickly amplify possible performance problems.
(1), JS position
When optimizing js in web code, it is suggested to put JS at the end of the page, which can speed up the opening of the page.
(2) merge JS
Merging js under the same domain name can improve the opening speed of web pages by reducing the number of network connections.
(3) LazyLoad technology
lazy
Load is a jQuery plug-in written in JavaScript, which can delay the loading of pictures in long pages, and pictures outside the viewable area of the browser will not be loaded until the user scrolls the page to its position.
(4) calling external JS code
We know that the current search engine still can't recognize js code. If a large number of JS codes appear in the website, it is difficult to collect them. What we need to do is to put Javascript code in the website in the form of external call, which will simplify the work of search engines and will not lead to invalid code involving the website.
Not only that, css code can also be used for external calls. At the beginning of the website, you can define the text and color of the website in the css code file, and try not to have too many style codes in the page code.
(5) Reduce the dependence of pages on JS.
Now, JS is not unfriendly to search engines. Although it is reported that search engines will not dislike JS, it is better to have more than one thing. Although JS can produce many effects, a large number of JS in a web page will affect the spider's crawling of the page and increase the volume of the web page, especially the key positions of the navigation bar and other pages. Try to use DIV+CSS design method.
5. Reduction of form labels
Form label is the most common code form in most online websites. The fundamental reason is that table is faster when building a website, but it also affects the post-optimization of the website.
Compared with the simple code website with div+css layout, it takes up more space. Therefore, when building a website, use tables as little as possible, and even if tables are to be used, use nested tables as little as possible to avoid garbled codes.
So, what is the current website made of? The first thing that many programmers think of is to use CSS to do it and use CSS to typeset it. This greatly reduces the number of tables in the page. However, the website can not be separated from the form, and some things must be used. There is nothing wrong with using tables, but many websites use nested tables. Generally, this table form will produce a lot of junk code for the website, and these junk codes are useless. This kind of code is also one of the codes that need to be streamlined in our website.
6. Code comments are omitted
Many programmers are used to writing a note that others can't understand. These codes are often for the cooperation of several programmers and are of no use to outsiders and search engines. On the contrary, it will bring some trouble to search engine spiders.
When we open the page code, we often see some comment codes, which are comments made by programmers to express the meaning of the code. In fact, these openings are unnecessary, because for search engines, they have no meaning, but will increase the capacity of page code, which is not good for the website. Why don't you just omit it?
7. Clear the redundant code in the page.
Some websites think that there are many blank codes on the page because of the code writing habits of the producers, such as blank codes, codes with repeated definitions of styles and fonts. Don't underestimate these small codes, accumulating too much will make our website extremely bloated.
Many websites use DIV+CSS, which defines the font, color and page layout of the text, but in other parts of the website, the font is redefined with style and font, which can be simplified without repeated definition.
8. Convert html control mode into CSS control.
Many web designers are used to controlling the content in tags. For example, in the img tag, the size of the picture is controlled by the width and height, and these codes are converted into external CSS as much as possible to make the webpage code finer.
9. Cache static resources
By setting the browser cache, the infrequently updated files such as css and js are cached in the browser, so that when the same visitor visits the website again, the browser can get css and js from the browser cache instead of reading them from the server every time, which speeds up the opening of the website to some extent and saves the server traffic.
10, Web page compression technology
As far as web page compression is concerned, I believe all webmasters are familiar with it. The main purpose is to enable server Gzip to compress pages and reduce the volume of elements, thus reducing data transmission and improving the loading speed of web pages. This function needs the support of the server. Gzip compression can generally compress web pages by 30%-80%, which is the most important optimization effect.
In a word, there are many ways to optimize the website through code optimization, and here I just talk about a few common ones.