Assume that the document pointer of the webpage has been obtained in VC ++, and there are two methods to access javascript: One is through document. script. parentwindow (directly use window below ). They all have the same effect. Next we will take window as the objective of the discussion.
In actual Web client script development, we have used JavaScript and VBScript together. Due to the powerful and flexible JavaScript, VBScript is rarely used in webpages, but in some cases, we have to use it again. For example, if you embed the RealPlayer player control in a webpage, you can use JavaScript to call the properties and methods of the RealPlayer control, but you can only use Vbscript to respond to events of the RealPlayer control. For more information, see the official help page of RealPlayer.Http://service.real.com/help/library/guides/extend/htmfiles/ch01_emb.htm#23018
Obviously, JavaScript and VBScript are two different script engines, and they must also exist in different engine instances in a page environment. However, we found that the variables and Methods declared in two different script engines can access and call each other. How is this implemented? We tried to list several technical points.
- Both support global objects. Window is the Global Object of the two. All the attributes and methods of the global object can be accessed without the object name. For example, you can use either window. Alert ("Hello World") or alert ("Hello World ");
- The window object contains references of the two instances.
- The window object implements the idispatchex interface, exposing all the attributes and methods in the two instances through the idispatchex interface, that is, let the external code think, the properties and methods defined in the script engine are also window objects.
For example, VBScript defines an echo method. Then, access the echo method in VBScript in JavaScript. Let's analyze how it works.
<Script language = "VBScript">
Sub echo (s)
Alert s
End sub
</SCRIPT>
<Script language = "JavaScript">
Echo ("Hello World ");
</SCRIPT>
First, the echo method defined in VBScript can be accessed as a window object method. The JavaScript engine first finds the echo method in the space of this instance. If it fails, it finds the Global Object window, finds it, And then executes it.
To sum up, we can think that window has three main functions in the script engine:
- Window is the Global Object of the script engine, which makes the code more concise when accessing the properties and methods in window. For example, window. Alert ("Hello World") and alert ("Hello World"); therefore, this role is dispensable.
- Window contains references of the script engine instance, and routes all attributes and methods of the script engine instance through the idispatchex interface. So that the variables and Methods declared in different script engines can access and call each other. If there is no special application environment that makes it impossible for us to use VBscript, for me, I will always simply use JavaScript. Therefore, this role is also dispensable.
- In VC ++, we can get the document pointer of the webpage. To access the attributes and methods in the script engine, you can use document. script or document. parentwindow (window ). Therefore, this role is also dispensable.
Therefore, the above design of window is not necessary, but it can make the code written more concise and the application more flexible.