JavaScript and C # Windows application interaction Methods _javascript Tips

Source: Internet
Author: User
First, the establishment of web pages




<meta http-equiv= "Content-language" content= "ZH-CN" >
<script language= "javascript" type= "Text/javascript" >
<!--methods provided to the C # program call-->
function MessageBox (message)
{
alert (message);
}
</script>


<body>
<!--calling C # method-->
<button onclick= "Window.external.MyMessageBox (' JavaScript access to C # code ')" >
JavaScript Access C # code </button>
</body>



II. Building Windows applications

1. Create a Windows Application project

2. Add WebBrowser controls to the Form1 form

3. Add above the Form1 class

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

This is to set the class to COM accessible. If you do not make this declaration, there will be an error. The error message is shown in the following illustration:



Such as:

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

public partial class Form1:form



4. Initializes the WebBrowser URL with objectforscripting two properties.

URL Properties: The page path displayed by the WebBrowser control

ObjectForScripting property: This object can be accessed by scripting code that is contained in the Web page that is displayed in the WebBrowser control.

Set the URL property to the URL path of the page you want to manipulate.

JavaScript uses window.external to invoke C # exposed methods. That is, the public method contained in an instance of the class set by the ObjectForScripting property. Examples of specific settings are as follows:

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

The page path displayed by the WebBrowser control

Webbrowser1.url = new Uri (file. FullName);

Sets the current class to be accessible by script

Webbrowser1.objectforscripting = this;



5. C # calls JavaScript method

Invokes the JavaScript method of the current Web page through the Invokescript method in the document property of the WebBrowser class. Such as:

Call the JavaScript MessageBox method and pass in the argument

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");

The page path displayed by the WebBrowser control

Webbrowser1.url = new Uri (file. FullName);

Sets the current class to be accessible by script

Webbrowser1.objectforscripting = this;

}



private void Button1_Click (object sender, EventArgs e)

{

Call the JavaScript MessageBox method and pass in the argument

Object[] objects = new OBJECT[1];

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

WebBrowser1.Document.InvokeScript ("MessageBox", objects);

}

Methods that are provided to JavaScript calls

public void Mymessagebox (String message)

{

MessageBox.Show (message);
}
}



dnew.cn Note: Original: http://www.cnblogs.com/xds/archive/2007/03/02/661838.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.