|
玄机论坛-专业的C#交流论坛 交流QQ群: 16885911如有疑问,请加入官方群询问
您需要 登录 才可以下载或查看,没有帐号?关闭注册
x
webbrowser与js交互 js调用C#方法
[mw_shl_code=csharp,true]
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);
}
}
}[/mw_shl_code]
Html部分:
[mw_shl_code=csharp,true]<html>
<head>
</head>
<body>
</body>
<script type="text/javascript" charset="utf-8">
function Run(str)
{
window.external.ShowMsg(str);
}
</script>
</html>[/mw_shl_code]
要注意的地方
要公开对象记得加上
[PermissionSet(SecurityAction.Demand, Name ="FullTrust")]
[ComVisible(true)]
|
|