Mutual debugging of winform controls webbrowser and JS scripts
Default category
2007-09-06 22:40:26
Read257
Comment0
Font Size:Large
Medium
Small
1) Call JS functions in C #
Modify the attributes of webbrowser so that C # can call the js method:
Webbrowser1.objectforscripting = this;
If you want to pass a value, you can define the object [] array.
The specific method is as follows:
First, define the method called by C # In JS:
Function messageaa (Message)
{
Alert (Message );
}
Call the js method messageaa in C #
Private void button#click (Object sender, eventargs E)
{
// Call the MessageBox method of JavaScript and input parameters
Object [] objects = new object [1];
Objects [0] = "C # Diao JavaScript ";
Webbrowser1.document. invokescript ("messageaa", objects );
}
2) Call the C # method in JS
Calling the C # method in JS is relatively simple:
First, define the method called by JS in C:
Public void mymessagebox (string message)
{
MessageBox. Show (Message );
}
Call the C # method in JS:
<! -- Call C # Method -->
<Button onclick = "window. External. mymessagebox ('javascript access C #Code') ">
Javascript access C # Code </button>