点击(此处)折叠或打开
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Runtime.InteropServices; //记得u这俩.
- using System.Security.Permissions;//记得u这俩.
-
- namespace test
- {
-
- [PermissionSet(SecurityAction.Demand, Name ="FullTrust")]
- [ComVisible(true)]//com+可见
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- webBrowser1.Document.InvokeScript("Run", new object[] { "CShareFunction" });
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- webBrowser1.ObjectForScripting = this;//具体公开的对象,这里可以公开自定义对象
- webBrowser1.Navigate(Application.StartupPath + "/dom.html");
- }
-
- public void ShowMsg(string msg)
- {
- MessageBox.Show(msg);
-
- }
-
- }
- }
点击(此处)折叠或打开
- <html>
- <head>
-
- </head>
- <body>
- </body>
-
- <script type="text/javascript" charset="utf-8">
- function Run(str)
- {
-
- window.external.ShowMsg(str);
- }
- </script>
- </html>
下面是官方的说法:
执行在 HTML 页面中定义的动态脚本函数。
重载此成员。有关此成员的完整信息(包括语法、用法和示例),请单击重载列表中的相应名称。
HtmlDocument.InvokeScript 方法
名称 | 说明 | |
---|---|---|
InvokeScript(String) | 执行在 HTML 页面中定义的动态脚本函数。 | |
InvokeScript(String, Object[]) | 执行在 HTML 页面中定义的动态脚本函数。 |
点击(此处)折叠或打开
- <HTML>
- <SCRIPT>
- function test(name, address) {
- window.alert("Name is " + name + "; address is " + address);
- }
- </SCRIPT>
- <BODY>
- </BODY>
- </HTML>
C#代码:
点击(此处)折叠或打开
- private void InvokeTestMethod(String name, String address)
- {
- if (webBrowser1.Document != null)
- {
- Object[] objArray = new Object[2];
- objArray[0] = (Object)name;
- objArray[1] = (Object)address;
- webBrowser1.Document.InvokeScript("test", objArray);
- }
- }