Example: Try to write friendly Javascript code _ Javascript tutorial

Source: Internet
Author: User
Javascript: Example: Try to write friendly Javascript code, Javascript tutorial

In the Search Engine's robot Search, the "text" such as type, text, and html have the highest degree of friendliness (except for text/xml at present ), the friendliness of text and javascript is not ideal. If the robot still needs to judge the DHTML code, the complexity is also high and it cannot be determined. Therefore, when programming DHTML, to improve code friendliness. The better way is to "simplify DHTML code into code without HTML as much as possible ". How can this sentence be understood?

E. g:

A javascript menu.
Method 1: Use the conventional programming method:

Var navi_menu = neverDHTMLmenu ();
/* AddItem method
* @ Pid
* @ Id
* @ Text
* @ Href
*/
Navi_menu.addItem ("0", "1", "home", "http://www.never-online.net ");
Navi_menu.addItem ("0", "2", "blog", "http://blog.never-online.net ");
Navi_menu.addItem ("0", "3", "music", "http://www.never-online.net/music ");
Navi_menu.init ("navigator_Container ");
Script

2. Use a friendly programming method for Search Engine
The following is a reference clip:

Onload = function (){
Var config = {
Container: document. getElementById ("navigator_Container ");
// And more configuration code
}
Var navi_menu = new neverCssDHTMLMenu (config );
Navi_menu.init ();
Script



  • Home

  • Blog

  • Music




Compared with method 1 and method 2, some HTML of the method is encapsulated in neverDHTMLmenu (), but this method does not have practical advantages, although we can still give CSS to this class.
In method 2, we can see that there are many advantages. For example, you can separate view from program to implement MVC on the client. In other words, the development efficiency can be improved.

Some may ask, in addition to menu, which programs can be separated by the above method?
As mentioned above, a large amount of HTML will be generated when there are many interactions with pages, which will affect the Search Engine's robot. This method can be used, of course, these are just about the friendliness of the Search Engine. Therefore, any code must be determined by the actual situation.

Or someone may ask, why does this increase development efficiency?
For example, after the artist completes the template (assuming that some of the relevant HTML code is written by this US Trade Union), it follows the xhtml standard (as shown above)
If I want to change the original home to the Default page, the artist should communicate with the programmer, say that the word of the menu should be changed, and the communication time will be longer in development. Therefore, this time must be included in the development progress. If you want to change the template, You have to communicate. Or, if the HTML originally generated by js is a menu made of table and needs to be revised, the program has to be rewritten. Not conducive to maintenance...

We recommend that you try this method. This means that JS is responsible for business implementation, while the view is still handled by HTML.
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.