Content introduction: With the With statement, you do not need to repeatedly specify the reference object when accessing object properties and methods. In the With statement block, all attributes and methods that are not recognized by JavaScript are related to the objects specified in the statement block. The syntax format of the With statement is as follows:
The With Object {Statements} object specifies the reference object when the object in the statement group is defaulted. Here we use the more familiar Document object to give an example of the With statement. For example, when using the write( ) or writeln( ) method related to the Document object, the following form is often used: document.writeln("Hello!"). If you need to display a large amount of data, the same document.writeln() statement will be used multiple times. At this time, you can put all statements with the Document object as the reference object into the With statement block like the following program, so as to achieve the purpose of reducing the number of statements. Here is an example of using With statement: <html><head><title>JavaScript Unleashed</title></head><body><script type="text/javascript"><!―with(document){write("Hello!");write("<br>The title of this document is: /"" + title + "/".");write("<br>The URL of this document is: " + URL);write("<br>Now you don't have to write the prefix of the document object every time!");}// --></script></body></html>This way, you can remove the Document prefix when using the document's methods and properties.