简体   繁体   中英

How can I call a Function of an JavaScript Object with WPF?

I have a WebBrowser instance in my WPF application (System.Windows.Controls.WebBrowser). In the website I have the following JavaScript code.

// Javascript   
myNamespace = {};
myNamespace.helloJavaScriptMETHOD = function() { alert("Hello"); };

helloJavaScriptFUNCTION = function() { alert("Hello); };

I can call helloJavaScriptFUNCTION with

// C#
webBrowser.InvokeScript("helloJavaScriptFUNCTION");

How can I call helloJavaScriptMETHOD without changing the WebSite/JavaScript? I have tried the following WITHOUT success:

// C#
webBrowser.InvokeScript("myNamespace.helloJavaScriptMETHOD");
webBrowser.InvokeScript("(function(){ return myNamespace.helloJavaScriptMETHOD; })");
webBrowser.InvokeScript("myNamespace.helloJavaScriptMETHOD()");

You may not be able to call a JavaScript method in a namespace directly but you can use eval . A similar technique was used to invoke JQuery. Look at How to call a jQuery function from .NET WebBrowser control using InvokeScript()? . It uses the wrong control but the technique may work here.

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