rak*_*aki 26 .net javascript c# controls webbrowser-control
我在C#中使用webBrowser控件来加载网页,需要调用一个返回字符串值的JavaScript函数.我有一个使用InvokeScript方法的解决方案,我尝试了很多,但一切都失败了.
Gid*_*don 34
你能说明什么失败吗?
我的下面的示例包含一个带有WebBrowser和Button的表单.
最后称为y的对象有句"我做了!".所以和我一起工作吧.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.DocumentText = @"<html><head>
<script type='text/javascript'>
function doIt() {
alert('hello again');
return 'i did it!';
}
</script>
</head><body>hello!</body></html>";
}
private void button1_Click(object sender, EventArgs e)
{
object y = webBrowser1.Document.InvokeScript("doIt");
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
您可以将参数发送到js函数:
// don't forget this:
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.DocumentText = @"<html><head>
<script type='text/javascript'>
function doIt(myArg, arg2, arg3) {
alert('hello again ' + myArg);
return 'yes '+arg2+' - you did it! thanks to ' +myArg+ ' & ' +arg3;
}
</script>
</head><body>hello!</body></html>";
}
private void button1_Click(object sender, EventArgs e)
{
// get the retrieved object from js into object y
object y = webBrowser1.Document.InvokeScript("doIt", new string[] { "Snir", "Raki", "Gidon"});
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
49663 次 |
最近记录: |