Home  >  Article  >  Web Front-end  >  How to interact with JavaScript and C# Windows applications_javascript skills

How to interact with JavaScript and C# Windows applications_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:12:011232browse

1. Create a web page




🎜> 
 
JavaScript access C# code




1. Create a Windows application project
2. Add the WebBrowser control in the Form1 form
3. Add the

[System. Runtime.InteropServices.ComVisibleAttribute(true)]

This is to set the class to be com accessible. Failure to make this declaration will result in an error. The error message is as shown below:



For example:

[System.Runtime.InteropServices.ComVisibleAttribute(true)]

public partial class Form1: Form



4. Initialize the Url and ObjectForScripting properties of WebBrowser.

Url property: The path of the web page displayed by the WebBrowser control

ObjectForScripting property: This object can be accessed by the script code contained in the web page displayed in the WebBrowser control.

Set the Url attribute to the URL path of the page that needs to be operated.

JavaScript calls methods exposed by C# through window.external. That is, the public methods contained in the instance of the class set by the ObjectForScripting property. Specific setting examples are as follows:

System.IO.FileInfo file = new System.IO.FileInfo("index.htm");

// Web page path displayed by the WebBrowser control

webBrowser1.Url = new Uri(file.FullName);

// Set the current class to be accessible by scripts

webBrowser1.ObjectForScripting = this;



5. C# calls the JavaScript method

Call the JavaScript method of the current web page through the InvokeScript method in the Document property of the WebBrowser class. For example:

// Call JavaScript’s messageBox method and pass in the parameters

object[] objects = new object[1];

objects[0] = "C# Access JavaScript script";

webBrowser1.Document.InvokeScript("messageBox", objects);



The complete code is as follows:


[ System.Runtime.InteropServices.ComVisibleAttribute(true)]

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

System.IO.FileInfo file = new System.IO.FileInfo("index.htm");

// Web page path displayed by the WebBrowser control

webBrowser1.Url = new Uri(file.FullName);

// Set the current class to be accessible by scripts

webBrowser1.ObjectForScripting = this;

}



private void button1_Click(object sender, EventArgs e)

{

// Call the JavaScript messageBox method and pass in the parameters

object[] objects = new object[1];

objects[0] = "C# access JavaScript script";

webBrowser1.Document.InvokeScript("messageBox", objects ; .Show(message);
}
}



Dnew.cn Note: Original text: http://www.cnblogs.com/xds/archive/2007/03 /02/661838.html
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn