0

I'm utilizing a System.Windows.Forms.WebBrowser (used in a separate .dll file) from my WPF application in order to print some HTML content in the background.
My problem is that the HTML elemnts are not being affected by the javascript in the page.
I know that there are no errors in the page because that if I copy its content after the DocumentCompleted event and paste it into a plain HTML file the script works.
My HTML template:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script>
    function loaded() {
        document.getElementById("content").innerHTML = "after javascript";
    }
</script>
</head>
<body onload="loaded()">
    <h1 id="content">before javascript</h1> <!--<= This value does not change-->
</body>
</html>

I use this HTML as follows:

private void PrintTest()
{
    string curDir = Directory.GetCurrentDirectory();
    string fullPath = Path.Combine(curDir, "PrintDocs\\HTMLPage1.html");

    string html = File.ReadAllText(fullPath);

    HtmlPrint.Print(html);
}

public static class HtmlPrint
{
    public static void Print(string html, string documentTitle = "")
    {
        var wb = new System.Windows.Forms.WebBrowser { DocumentText = html };
        wb.DocumentCompleted += wb_DocumentCompleted;
    }

    private static void wb_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
    {
        var wb = ((System.Windows.Forms.WebBrowser)sender);

        wb.Print();
        wb.Dispose();
    }
}

What am I missing here?

Yoav
  • 3,326
  • 3
  • 32
  • 73
  • there was a copy \ paste problem with my javascript call. fixed it but the problem remains – Yoav Oct 19 '15 at 10:37
  • @Izzy I guess you mean `wb.Document.InvokeScript("loaded");` because mysteriously it works. put this as an answer for future readers. – Yoav Oct 19 '15 at 10:51
  • Possible duplicate of [WebBrowser Control DocumentCompleted after iframe & Javascript completion](http://stackoverflow.com/questions/19371893/webbrowser-control-documentcompleted-after-iframe-javascript-completion) – Amit Oct 19 '15 at 10:54

3 Answers3

1

You forgot the Braces in onload:

<body onload="loaded()">
FlorianR.
  • 66
  • 4
  • nice catch but it was just a test that I've made (I'm not that familiar with js), the results are the same in both cases – Yoav Oct 19 '15 at 10:39
0

I think you have to change onload="loaded" to onload="loaded()"

Med.Amine.Touil
  • 1,225
  • 2
  • 11
  • 15
0

As mentioned in my comments you can call the InvokeScript() method from your C# code and pass in your js function name as which should do the trick.

wb.Document.InvokeScript("loaded");
Izzy
  • 6,740
  • 7
  • 40
  • 84
  • I think the javascript executes after the DocumentCompleted event. If I have alert("HELLO!") in the page, the popup appears after the DocumentCompleted handler has finished. This caused me problems, since I wanted to access values set by javascript on the page. I ended up triggering a timer in the DocumentCompleted handler that waited a second; on the timer_tick handler, I could then grab the now populated value. – RickySpanish Jun 29 '17 at 10:48