简体   繁体   中英

C# WPF WebBroswer Control: How to use JavaScript

I am using WPF WebBrowser control and I want to acces some of the JavaScript functions but there is the problem.

I can use InvokeScript and execute browser.InvokeScript("alert", "Hello");q but how to get element by ID or by TAG and how to assign that element to javascript var?

Example: Javascript: var elements = document.getElementsByTagName("embed"); elements[0].doSomething();

C#: How?

I tryed everything but nothing worked. Can anyone help me :(

Quite a late answer, but if anyone else needs it:

The direct C#: http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementsbytagname.aspx

HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("embed");
foreach (HtmlElement elem in elems)
    {
        elem.InvokeMember("doSomething");
    }

The alternative: http://msdn.microsoft.com/en-us/library/a0746166

Basically you should create a function in JS:

var myCustomFunc = function(tagName) {
  var elements = document.getElementsByTagName(tagName); elements[0].doSomething();
}

And then call it from C# with

webBrowser1.Document.InvokeScript("myCustomFunc ", new String[] { "embed" });

The variable "tagName" gets replaced with "embed"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM