2013-07-17 102 views
1

我使用的是Gekofx浏览器,因为我的html文件无法使用默认的浏览器控件。我如何使用C#与Gekofx浏览器调用JavaScript函数

到目前为止,我正在使用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

您可以随时调用JavaScript这样的:

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

首先,谢谢你的回答。但我猜在我的代码中是错误的或者仍然不完整。因为这条线不做任何事情。这是我的JavaScript代码:'

的Hello World!

'有什么可以做的吗? –

+0

然后你用mybrowser.Navigate(“javascript:test('Test')”);? – Jordy

+0

是的。没有反馈。但是它在将下面的代码部分放入我的'btn_test_Click'之后工作:'myBrowser.Navigate(path); MessageBox.Show(“Stop!”); myBrowser.Navigate(“javascript:test('Test')”);' 我不是没有为什么,但它的工作原理。你能解释我为什么? –

0

大厦@约迪的回答以上,请致电:

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

prefferably在文档完整的事件处理程序,以允许页面首先加载。

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