A group of skills of DHTML Mark DaviSmicrosoft Corporation Abstract: This article illustrates some significant impact of certain DHTML functions for performance and provides some techniques for improving DHTML page performance. Directory 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 Dynamic Properties Data Binding Do not set an Expando property in the Document object Avoid switching and style rules to find parents, first folding text range Other information Introduction Dynamic HTML (DHTML) Introduction in Microsoft® Internet Explorer 4.0, making web authors and developers can 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 changes on the DHTML web page, improve performance, 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. Show slow: divupdate.innerhtml = ""; 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 the Innertext DHTML object model to access the HTML element's text content through the Innertext (English) property, while 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. Show slow: var node;
FOR (var i = 0; i <100; i )
{
Node = Document.createElement ("span");
Node.Appendchild (Document.createTextNode ("Using CreateTextNode ()));
Divupdate.Appendchild (Node);
}
VAR Node;
FOR (var i = 0; i <100; i )
{
Node = Document.createElement ("span");
Node.innertext = "Use Innertext Properties";
Divupdate.Appendchild (Node);
}
Using the DOM Add a single element As mentioned earlier, the access method of applying the HTML text will cause the HTML analyzer to cause the 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>");
}
VAR Node;
FOR (var i = 0; i <100; i )
{
Node = Document.createElement ("span");
Node.innertext = "Use INSERTADJACENTELEMENT ()";
Divupdate.Insertadjacentelement ("BeforeEnd", Node);
}
Options in the extended Select element are an exception to the case where the last use of the HTML text method is used. 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 show slow: var.
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 = "