0

I am trying to run Javascript function inside ASP.Net Web Application, and i am using WebBrowser to do this:

JavaScriptSerializer js = new JavaScriptSerializer();
Dictionary<string, object> mainDic = (Dictionary<string, object>)js.DeserializeObject(fulljsFile);

String base64Content = (String)mainDic["html"];
String html = Base64.Base64Decode(base64Content);

Thread thread = new Thread(delegate()
{
    WebBrowser browser = new WebBrowser();

    browser.Navigate("about:blank");
    if (browser.Document != null)
    {
        browser.Document.Write(string.Empty);
    }

    browser.Document.Write(html);

    string name = (string)browser.Document.InvokeScript("extract");
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();

This is the html var :

<html><script>
function extract()
{
    return 'Steve';
};
    </script>
</html>

The problem is that name is empty instead of Steve.

When i run it locally on my IIS server it's working, but when i upload it to my server it's fail to return something to name var.

YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • @CodeCaster I need to run JavaScript function in the code – YosiFZ Feb 04 '16 at 11:27
  • There are better ways to do that, see for example [Embedding JavaScript engine into .NET](http://stackoverflow.com/questions/172753/embedding-javascript-engine-into-net). Anyway non-repro, this code does not "return" anything. Please show your actual code, and the exception handling you use. I'm fairly sure WinForms's WebBrowser doesn't like to be run on a server, as a service. – CodeCaster Feb 04 '16 at 11:28

1 Answers1

1

I am wondering if the document has loaded into the browser at the point that you try and run the script. So, can you try handling the navigation complete event and run the script from there instead.

Justin Harvey
  • 14,446
  • 2
  • 27
  • 30