We have a windows application that contains an ActiveX WebBrowser control. As part of the regular operation of this application modifications are made to the pages that are displayed by the ActiveX WebBrowser control. Part of these modifications involve setting a JavaScript variable in a web page being loaded into the ActiveX WebBrowser.
We need to initialize this variable within C# (originally, VB6 code was initializing the value). The value of this variable is a COM-visible class object.
However, for simplicity we've reduced the problem to setting a string value. Our original page involves frames and the like but the same problems happens in a page like this:
<HTML>
<HEAD>
<TITLE>Test</TITLE>
<SCRIPT type="text/javascript">
var field = 'hello world';
</SCRIPT>
</HEAD>
<BODY>
<input type="button" value="See field" onclick="javascript:alert(field);"/>
</BODY>
</HTML>
We want to access the field variable and assign a value to it. In VB6 the code for this was pretty straightforward:
doc.Script.field = 'newValue'
However, in C# we've had to resort to other tricks, like this:
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSet(Script, null, "field",new object[] { "newValue"},null, null);
The point of the page is to test whether our variable was properly assigned by C#. Clicking on the button should yield whatever new value was injected by C#. So for example, clicking on the button in the page we get an alert showing: "newValue".
That works the first time, but it doesn't work if we reload the page. On subsequent calls we cannot set the value of the variable field
.
Has anyone had any experience doing this type of operation before?