调用在C#中的WebBrowser控件的Javascript函数 [英] Calling a Javascript function in the C# webBrowser control

查看:486
本文介绍了调用在C#中的WebBrowser控件的Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是 web浏览器在C#中控制加载一个网页,需要调用一个JavaScript函数,该函数返回一个字符串值。我有一个解决方案,使用 InvokeScript 的方法,我尝试了很多,但一切都得到了失败...... plz帮助我

I am using the webBrowser control in c# to load a webpage and need to call a JavaScript function that returns a string value. I got a solution to use the InvokeScript method, and i tried a lot , but everything got failed...... plz help me

推荐答案

您可以指定哪些失败?

我的示例下面由一个web浏览器和一个按钮的形式。

My sample below consists of a form with a WebBrowser and a Button.

在年底对象调用y的句子我做到了!。所以我它的工作原理。

The object called y in the end has the sentence "i did it!". So with me it works.

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

这篇关于调用在C#中的WebBrowser控件的Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆