1

With a WPF WebBrowser, I'm trying to use a JavaScript method with the code:

WebBrowser.InvokeScript("eval", new object[] { if(typeof myMethod == 'function') { myMethod('param'); } });

My method :

function myMethod(param) {
    alert(param);
}

If I put this method directly in my HTML file, it works. But if the method is in a separate file, it doesn't work. The JavaScript file is imported like so:

<script type='text/javascript' src='../script.js'></script>    

And it works if I use methods of the script from my HTML page. The problem certainly is that InvokeScript tries to use a method inside my HTML and doesn't import the script.

How can I make this work?

Chris Haines
  • 6,445
  • 5
  • 49
  • 62
Gab
  • 1,861
  • 5
  • 26
  • 39
  • You are probably invoking it too early, before the browser has downloaded and run the external script file. – Dark Falcon Apr 15 '14 at 15:05
  • In fact, my page is fully loaded, then I click on a button that launch a C# function, then the InvokeScript() is called.... – Gab Apr 15 '14 at 15:08
  • tried your code but I can't compile new object[] { if(typeof myMethod == 'function') { myMethod('param'); } } – Dmitry Apr 15 '14 at 18:46
  • If I just call WebBrowser.InvokeScript("myFunction"); it works fine from separate file. – Dmitry Apr 15 '14 at 18:56
  • ok you just forgot to put commas. But your code works fine for me. Cheack that your script is in a folder above the html file. – Dmitry Apr 15 '14 at 19:00
  • Thank you for the answer! I can use other methods from the script so it is well imported... I'll try with a new project an try to find a solution... At least I know that it should work... – Gab Apr 16 '14 at 06:27
  • 1
    Did you get to solve it? I have the same problem – arianoo Jun 13 '14 at 12:44
  • does anyone has a solution? – Sven Borden Apr 07 '16 at 16:33

0 Answers0