2009-09-17 40 views
23

Bir web sayfasını yüklemek için C# webBrowser denetimini kullanıyorum ve bir dize değeri döndüren bir JavaScript işlevini çağırmam gerekiyor. InvokeScript yöntemini kullanmak için bir çözüm buldum ve çok denedim, ancak her şey başarısız oldu.C# webBrowser denetiminde bir Javascript işlevi çağırma

cevap

31

Neyin başarısız olduğunu belirtebilir misiniz?

Benim örnek altında bir WebBrowser ve Button ile bir form oluşur.

Sonunda y adı verilen nesne "ben yaptım" cümlesine sahiptir. Yani benimle çalışır.

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

Sen js işleve argümanlar gönderebilirsiniz:

// 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"}); 
    } 
} 
İlgili konular