Javascript to retrieve all images of the document

Source: Internet
Author: User

Javascript to retrieve all images of the document
Function updateUI (){
Var imgs = document. getElementsByTagName ("img ");
For (var I = 0, len = imgs. length; I <len; I ++ ){
Imgs [I]. title = document. title + "image" + I;
}
 
Var msg = document. getElementById ("msg ");
Msg. innerHTML = "Update complete .";
}

This function may seem completely normal, but it has three reference global document objects. If there is a page

The number of images is large, and dozens or hundreds of times can be executed in the circular reference file.

Chain. By creating a Document Object point for a local variable, you can add a limit that some global searches only have one

Function performance:

Function updateUI (){
Var doc = document;
Var imgs = doc. getElementsByTagName ("img ");
For (var I = 0, len = imgs. length; I <len; I ++ ){
Imgs [I]. title = doc. title + "image" + I;
}
 
Here, the first document object is stored in the local document variable. This document variable is then stored in the entire

The rest of the code. Only one global function is found here, which is faster than the previous version.

. A good rule of thumb is that any global storage object is used as a local variable more than once in a function.

.
 
Avoidance and Declaration


With statements should be avoided in performance. Similar function, it has the ability to create its own range for statements, so it is added

The length of the range chain in the executed code. The statement of the executed Code ensures that the code runs faster than the execution speed,

This is because of the additional steps for searching for a range chain. This is a rare statement that is required because it is mainly used to eliminate multiple

Remaining characters. In most cases, local variables can be used to accomplish the same thing without introducing a new range.

. The following is an example:

Function updateBody (){
With (document. body ){
Alert (tagName );
InnerHTML = "Hello world! ";
}
}

 

Function updateBody (){
Var body = document. body
Alert (body. tagName );
Body. innerHTML = "Hello world! ";
}

This Code uses document. body to make it easier to speak. The same effect can be achieved by using a local variable.

, As follows:
Var msg = doc. getElementById ("msg ");
Msg. innerHTML = "Update complete .";
}

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.