WebBrowser.InvokeScript() Throws Error: 80020006

I am developing a Windows Phone 8 App and i am new to Windows Phone 8.

I am using WebBrowser Control and try to load the html page having javascript.

On button_Click event i am calling Webbrowser.InvokeScript("JavascriptFunctionName"), but it always throws the error: 80020006, even though the WebBrowser.isScriptEnabled = true.

Below is the code

Html Page: MyHtmlPage.html

<!DOCTYPE html>
<html>
<head title="test">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
    html {
        height: 100%;
    }

    body {
        height: 100%;
        margin: 0;
        padding: 0;
    }

    #map-canvas {
        height: 100%;
    }
</style>

<script type="text/javascript">
    function initialize() {

        var helloText = document.getElementById("Hello");
        helloText.textContent = "Initialized";

        return ("Initialize");

    }

</script>
</head>
<body>
    <div id="test-canvas" />
    <label id="Hello">Hello</label>
</body>
</html>

Cs File : MainPage.cs

private string TestUri = @"Html/Test.html"; 
private string stringHtml = string.Empty;

public MainPage()
{
    InitializeComponent();
    WebBrowser.IsScriptEnabled = true;
    WebBrowser.IsGeolocationEnabled = true;
}

private void WebBrowser_Loaded(object sender, RoutedEventArgs e)
{            
    var resource = App.GetResourceStream(new Uri(TestUri, UriKind.Relative));
    stringHtml = new StreamReader(resource.Stream).ReadToEnd();
    WebBrowser.NavigateToString(stringHtml);
}

private void Button_Click(object sender, RoutedEventArgs e)
{
   try
   {
       //Error 
       string returnValue = WebBrowser.InvokeScript("initialize");
   }
   catch(Exception ex)
   {
   }

}

Help me.. Thanks.

Upvotes: 2

Views: 1938

Answers (1)

noseratio
noseratio

Reputation: 61686

Scripts are not getting executed when you use NavigateToString under WP8. The following test proves this, the body background color doesn't turn red.

<script type="text/javascript">
    document.body.style.backgroundColor = "red";

    window.initialize = function() {
        document.body.style.backgroundColor = "blue";

        var helloText = document.getElementById("Hello");
        helloText.textContent = "Initialized";

        return ("Initialize");
    }
</script>

One possible solution is to create a temporary file in the isolated storage, as described here.

Another option is to navigate to a blank page with Navigate("about:blank"), handle WebBrowser.LoadCompleted event, then inject the desired HTML and scripts, as described here.

Upvotes: 1

Related Questions