Using an Enumerator in JavaScript
I have the following VBScript code, which I would like to express instead in JavaScript:
Sub GxUIProxyVB_OnLogon
Dim EntityProxy
For Each EntityProxy In GxUIProxyVB.ListEntityProxy
MsgBox EntityProxy.Name
Next
End Sub
To give context this code is a post-logon event handler of an ActiveX control's logon event. The ActiveX control is hosted in a web page running in Internet Explorer 8. The user of the web page triggers this code's execution by successfully logging on via the ActiveX control's logon mechanism.
In the code GxUIProxyVB is a reference to the ActiveX control's object embedded in the DOM via an HTML element.
This is the JavaScript I have so far:
function GxUIProxyVB::OnLogon()
{
var EntityProxy;
// For Each EntityProxy In GxUIProxyVB.ListEntityProxy
// alert(EntityProxy.Name);
// Next
}
I have commented out the part I am struggling with: enumerating the value of GxUIProxyVB.ListEntityProxy.
This screen shot from IE8's watch list shows the members of the ListEntityProxy object
As a workaround I realize I could just leave the code in VBScript as the users will probably only be开发者_JAVA技巧 using Internet Explorer to access this content, but I'd rather have it in JavaScript for code maintainablity. (I don't want future web developers who maintain this code to need to be proficient in VBScript.)
You'll have to use an Enumerator object:
function gxUIProxyVB_OnLogon() {
var entityProxy;
for (var enr = new Enumerator(GxUIProxyVB.ListEntityProxy); !enr.atEnd(); enr.moveNext()) {
entityProxy = enr.item();
alert(entityProxy.Name);
}
}
精彩评论