In the process of developing WinForm programs using C #, we often encounter browser controls embedded with a webbrowser. Many times, we need to control the way the page is displayed in the program, or call a certain JS function in the Web page, in turn, it is possible that the Web page also needs to invoke functions in the program to implement certain functions. Now let me explain how to interact with each other.
The program calls the JS script as follows:
JS Script code:
<script language=”javascript”>
function ShopXG(infor)
{
alert(‘Programming portal - www.bianceng.cn’);
return;
}
</script>
C # code calls are as follows:
Using System.Security.Permissions;
Note: The following two lines need to be added before the class definition, otherwise the call fails!
[PermissionSet (SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute (True)]
The function called:
WebBrowser1.Document.InvokeScript ("Shopxg", new string[] {' ssss '});
Example of calling C # functions in JS:
The C # functions are as follows:
public void ShowMessage(string message)
{
MessageBox.Show(“Programming portal - www.bianceng.cn”);
}
JS in the call method:
<script language=”javascript”>
function InvokeFunc()
{
window.external.ShowMessage(‘呵呵’);
}
</script>