How Do I Interact with a Web Page Running in a Browser Control? (Magic xpa 2.x)
When you are displaying a web page in a Rich Client Browser control, you can have that web page send you information, and you can send information to the web page.
A web page can send information to your Rich Client program by using a special event, called MGExternalEvent. This is coded into the code of the web page. For instance, here is a VBScript that sends a date argument back to Magic xpa:
<SCRIPT LANGUAGE=”VBScript”>
// This VBScript segment handles the click event of the Calendar object.
// Upon a click on the object the MGExternalEvent function is called with
// the expected arguments.
// This allows external modules on the page to interact with the Magic xpa
// engine.
Sub Calendar1 click()
call window.MGExternalEvent(
document.Calendar1.Day & ‘|’
document.Calendar1.Month & ‘|’
document.Calendar1.Year )
end Sub
</SCRIPT>
This will raise MGExternalEvent, sending the three date values in one string delimited by ‘|’. This is important because only one parameter, a BLOB, can be returned.
Now, in the Rich Client program, you need to handle that event. This is done by creating a handler for the Internal event called External Event.
All that remains is to parse the BLOB according to what was sent. In this example, the BLOB is text, so it is defined as Ansi, allowing the use of the DVal function directly on the BLOB. The various string parsing functions in Magic xpa -- such as StrToken() and the conversion functions -- are quite useful for this kind of parsing.
You can also send data and commands to the browser interactively.
One way to do this is to use the BrowserScriptExecute() function. This sends and executes a script inside the browser. By default, JavaScript is used, but you can specify other languages too.
You can also change the entire content of the page by using the BrowserSetContent() function. This replaces all the HTML that the Browser control is displaying. Similarly, BrowserGetContent() will return the entire contents of the page.
Note: You can alternatively use the .NET browser control, System.Windows.Forms.WebBrowser, which, like all .NET controls, gives you more low-level control but has a higher learning curve.
The Rich Client Samples project (program RRC07)