C# Webbrowser 컨트롤에서 JavaScript 함수 호출
-
08-07-2019 - |
문제
나는 그것을 사용하고있다 웹 브라우저 C#의 컨트롤 웹 페이지를로드하려면 문자열 값을 반환하는 JavaScript 함수를 호출해야합니다. 나는 그것을 사용할 솔루션을 얻었다 스크립트를 호출합니다 방법, 그리고 나는 많이 시도했지만 모든 것이 실패했습니다.
해결책
실패한 것을 지정할 수 있습니까?
아래 샘플은 웹 브라우저와 버튼이있는 양식으로 구성됩니다.
결국 Y라는 개체에는 "내가 했어요!"라는 문장이 있습니다. 그래서 나와 함께 작동합니다.
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");
}
}
다른 팁
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"});
}
}
제휴하지 않습니다 StackOverflow