Accelerate a group of skills of DHTML
Mark DaviSmicrosoft Corporation
Abstract: This article illustrates some of the significant impact of certain DHTML functions and provides some techniques for improving DHTML page performance.
table of Contents
Introduction Batch DHTML Change Use Innertext Use the DOM to add a single element to extend the option in the select element with a DOM update table, use multiple times, do not use the dynamic property data binding, do not set an expando attribute in the Document object to avoid Change classes and style rules Fold before folding with other information
Introduction
Dynamic HTML (DHTML) Introduction in Microsoft® Internet Explorer 4.0, allows web authorities and developers to use new programming models. Since then, the Web author takes advantage of this powerful feature to provide dynamic content, style, and positioning, so that web users can experience rich interactive features. The flexibility of DHTML makes it usually in a variety of ways to achieve your idea. Understand the HTML analysis of Internet Explorer and how to deal with the request, help you determine the best way to complete your work. This article describes some of the significant impact of certain DHTML functions and provides some techniques for improving page performance.
Batch DHTML change
In the DHTML web page, the most efficient way to improve performance is to improve changes to HTML content on the page. There are a variety of ways to update the web page and understand this is very important. From the customer's feedback, the web author can apply the HTML text block, or you can access individual HTML elements by using the DHTML object model (English) or W3C Document Object Model (DOM) (English). Whenever you change HTML content, Internet Explorer's HTML analysis and display components must reassure the internal performance form of the page, recalculate document layout and document stream, and display these changes. Although the actual performance is determined by the content of the web page and the changes you make, these operating costs are relatively large. If you apply an HTML text block, instead of individual access elements, you must call the HTML analyzer, which will result in additional performance overhead. Method and attributes receiving HTML text include INSERTADJACENTHTML (English) and Pastehtml (English) methods, and InnerHTML (English) and OuterHTML (English) attribute.
Tips 1: Change the HTML content in a scripting function. If your design uses multiple event handles (eg, responding to mouse movements), you should centralize.
Another important fact that HTML analysis and display components is: Once script returns control (for example, when the script event handler exits, or when the SETTIMEOUT (English) is called), the component will recalculate the layout and display the web page. . Now, you have learned how Internet Explorer handle changes, which will begin to improve the performance of the web page.
Tip 2: Create an HTML string and make a change in the document, not multiple updates. If the HTML content is not necessary, consider using the Innertext (English) attribute.
In the following example, the slower method is called each time the InnerHTML property is set. To improve performance, you can create a string first, and then assign it to the InnerHTML property.
Please display
slow:
Divupdate.INNNERHTML = "";
FOR (var i = 0; i <100; i )
{
Divupdate.innerhtml = " This is a slower way! span>";
}
fast:
Var str = ""
FOR (var i = 0; i <100; i )
{
Str = " This method is faster than using the string! span>";
}
Divupdate.innerhtml = STR;
For more information, see Dynamic Content (English).
Use Innertext
The DHTML object model accesses the text content of the HTML element through the Innertext (English) property, and W3C DOM provides a separate sub-text node. Directly update the content of the element directly through the Innertext property, faster than calling the Dom CreateTextNode (English) method.
Tips 3: Update the text content using the Innertext property.
The following example shows how to use Innertext properties to improve performance.
Please display
slow:
Var node;
FOR (var i = 0; i <100; i )
{
Node = Document.createElement ("span");
Node.Appendchild (Document.createTextNode ("Using CreateTextNode ()));
Divupdate.Appendchild (Node);
}
fast:
Var node;
FOR (var i = 0; i <100; i )
{
Node = Document.createElement ("span");
Node.innertext = "Use Innertext Properties";
Divupdate.Appendchild (Node);
}
Add a single element using DOM
As mentioned earlier, the access method of applying HTML text will result in calling an HTML analyzer, thereby reducing performance. Therefore, adding an element using CreateElement (English) and INSERTADJACENTELEMENT (English) method is fast than calling the InsertJacenthtml method.
Tips 4: Call CreateElement and InsertAdjacentelement methods are fast than calling the InsertAdJacenthtml method.
Batch DHTML update and calls an InsertJacenthtml method to improve performance, but sometimes it is more efficient to create elements directly through DOM. In the following scenarios, you can try these two methods and determine which one is faster.
Please display
slow:
FOR (var i = 0; i <100; i )
{
Divupdate.insertadjacenthtml ("Beforeend", " Using INSERTADJACENTHTML () span>");
}
fast:
Var node;
FOR (var i = 0; i <100; i )
{
Node = Document.createElement ("span");
Node.innertext = "Use INSERTADJACENTELEMENT ()";
Divupdate.Insertadjacentelement ("BeforeEnd", Node);
}
Expand the options in the Select element
For the case of the last rule using the HTML text method, it is an exception to the case of adding a large number of options (English) elements. At this time, the use of the InnerHTML attribute is higher than that in calling CreateElement methods.
Tips 5: Use InnerHTML to add a large number of options to the SELECT element.
Use string connection operations to create an HTML text of the SELECT element, and then use this tip to set the InnerHTML property. For options that are particularly large, string connection operations also affect performance. In this case, create an array and call the Microsoft JScript® Join (English) method to perform the final connection of the Option element HTML text.
Please display
slow:
VAR OPT;
Divupdate.innerhtml = "
FOR (var i = 0; i <1000; i )
{
OPT = Document.createElement ("option");
SELUPDATE.OPTIONS.ADD (OPT);
Opt.innertext = "Part" i "item";
}
fast:
Var str = "