2009-09-17 7 views
23

webBrowser 컨트롤을 C#에서 사용하여 웹 페이지를로드하고 문자열 값을 반환하는 JavaScript 함수를 호출해야합니다. InvokeScript 메서드를 사용하는 솔루션이 있는데 많은 시도를했지만 모든 것이 실패했습니다.C# WebBrowser 컨트롤에서 JavaScript 함수 호출

답변

31

실패한 사항을 지정할 수 있습니까?

아래 샘플은 웹 브라우저와 단추가있는 양식으로 구성되어 있습니다.

끝에 y라는 개체에는 문장 "i did it!"이 있습니다. 그래서 나와 함께 작동합니다.

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

당신은 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"}); 
    } 
} 
관련 문제