如何使用gekofx浏览器使用c#调用javascript函数

时间:2013-07-17 10:02:57

标签: c# javascript html

我使用的是Gekofx浏览器,因为我的html文件无法使用默认的webbrowser控件。

到目前为止,我使用 ObjectForScripting 从我的C#项目中调用javascript代码。但我无法使用Gekofx浏览器调用任何内容。

我只是想将一些数据发送到我的html文件并使用Gekofx浏览器显示它。它有可能吗?

对于沉思,这里是我的代码:

GeckoWebBrowser myBrowser;

    public Form1()
    {
        InitializeComponent();

        String path = @"C:\tools\xulrunner\xulrunner-sdk\bin";

        Console.WriteLine("Path: " + path);

        Skybound.Gecko.Xpcom.Initialize(path);

        myBrowser = new GeckoWebBrowser();
        myBrowser.Parent = this;
        myBrowser.Dock = DockStyle.Fill;
    }

    private void btn_go_Click(object sender, EventArgs e)
    {
        // like the normal browsers
        myBrowser.Navigate(tbx_link.Text);
    }

    private void btn_test_Click(object sender, EventArgs e)
    {
        // getting the link to my own html file
        String path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Webpage");
        path += "\\Webpage.html";myBrowser.Navigate(path);

    }

我希望你理解我的意思。谢谢!

2 个答案:

答案 0 :(得分:2)

你可以随时调用这样的javascript:

mybrowser.Navigate("javascript:YourJavascriptFunction('yourArgument1', 'youArgument2')");

答案 1 :(得分:0)

在@ jordy的答案基础上,请致电:

mybrowser.Navigate("javascript:YourJavascriptFunction('yourArgument1', 'youArgument2')");

最好在Document complete事件处理程序中允许页面首次加载。

 void myBrowser_DocumentCompleted(object sender, Gecko.Events.GeckoDocumentCompletedEventArgs e)
    {
         myBrowser.Navigate("javascript:YourJavascriptFunction('yourArgument1', 'youArgument2')");
    }