0

I have made previous posts about my custom visualization not working in Spotfire: https://stackoverflow.com/questions/25390099/awesomium-javascript-handler-being-called-indefinitely Returning value to C# function from Javascript not working in Awesomium

and I have finally narrowed it down to the offending line.

In my document, I load a source script:

<script src="http://d3js.org/d3.v3.min.js"></script>

This seems to break my entire custom visualization; it infinitely tries to reload the page, from what I've seen. Here is my C# code:

        private void WebViewOnDomReady(object sender, EventArgs eventArgs)
{
    webView.DomReady -= WebViewOnDomReady;

    webView.CreateObject("jsobject");
    //webView.SetObjectCallback("jsobject", "callNETNoReturn", JSHandler);
    webView.SetObjectCallback("jsobject", "callNETWithReturn", JSHandler);
    //webView.ExecuteJavascript("myMethod()");
    var result = webView.ExecuteJavascriptWithResult("myMethodProvidingReturn('foo')");
    MessageBox.Show("Stuff:" + result.ToString());
}

private void JSHandler(object sender, JSCallbackEventArgs args)
{
    var result = webView.ExecuteJavascriptWithResult("myMethodProvidingReturn('foo')");

    MessageBox.Show(result.ToString());
    MessageBox.Show("Got method call with no return request");
}

And here is my Javascript code:

<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
function myMethod() {
        document.write("In myMethod, calling .NET but expecting no return value.<br/>");

        jsobject.callNETNoReturn();
    }



    function myMethodExpectingReturn() {
        document.write("In myMethodExpectingReturn, calling .NET and expecting return value.<br/>");

        var returnVal2 = jsobject.callNETWithReturn("foo");
        document.write("Got value from .NET: " + returnVal2 + "<br/>");
    }



    function myMethodProvidingReturn(whatToReturn) {
        var returnVal = whatToReturn + "bar";
        document.write("Returning '" + returnVal + "' to .NET.");

        return returnVal;
    }
</script>

Interestingly enough, the HTML loads fine if I don't try and call a Javascript function and get the return value in C#. However, when I try to return the result of the JS function and print it in C#, including the script src line breaks my entire code; it infinitely returns a blank message judging from the MessageBoxes that I have set.

This is completely baffling me, as it seems to mean that the HTML is being loaded over and over again. Setting the script src tag, for some odd reason, causes this infinite loop.

What exactly is happening?

Thanks

Community
  • 1
  • 1
Arthur Lee
  • 161
  • 1
  • 2
  • 12
  • Don't use `document.write` it can overwrite the page: http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – Halcyon Aug 25 '14 at 15:42
  • If `document.write()` is called AFTER the page has finished parsing, then it will clear the entire document and start a new empty document. I don't know from what you have posted if this is relevant to your issue or not, but if you are calling `document.write()` after the page has finished loading, that will cause problems. – jfriend00 Aug 25 '14 at 15:45
  • This isn't the cause of the issue. document.write is only there for testing; I am well aware that it writes over the Scatterplot Matrix that I am making on the page. This is just for testing purposes. However, I can confirm that commenting out the document.write calls don't affect the problem. – Arthur Lee Aug 25 '14 at 15:51
  • As if it got confusing enough already, changing stuff in myMethodExpectingReturn() affects things when I call myMethodProvidingReturn(whatToReturn). Logic seems to not work here. – Arthur Lee Aug 25 '14 at 16:37

0 Answers0