3

I am struggling with execution of Javascript methods on a Webbrowser control in WP8 application . XAML i used is

<phone:WebBrowser IsScriptEnabled="True"  x:Name="mainBrowserControl"  Grid.Row="2"/>            

And on button click event i ad the external javascript file testjs.js (This file is a part of aplication package and is inside the xap file) to the head section of currently loaded html page like

        string root = Package.Current.InstalledLocation.Path;           
        string jsfile_path = string.Format("{0}/testjs.js", root);

        var script = string.Format(@"var script=document.createElement('script');
                                     script.src=""{0}"";
                                     document.getElementsByTagName('head')[0].appendChild(script);", jsfile_path);
        mainBrowserControl.InvokeScript("eval", script);

Then on another button click even i tried to execute one javascript method inside the testjs.js file like

mainBrowserControl.InvokeScript("eval", "callMe();");

But it returns the error

An exception of type 'System.SystemException' occurred in Microsoft.Phone.Interop.ni.dll but was not handled in user code

Additional information: An unknown error has occurred. Error: 80020101.

The contents inside testjs.js file is just 3 simple functions. My ultimate aim is to interact (send and receive value to and from javascript functions)with an external javascript file like testjs.js from my application

 function callMe() {   
alert("hello called");
}
function showSum(val1, val2) {
alert(val1 + val2);
}
function addTwo(val1,val2) {
return val1+val2;
}

How can i call these methods on testjs.js from my windows phone app

Sebastian
  • 4,625
  • 17
  • 76
  • 145
  • 1
    possible duplicate of [How to inject Javascript in WebBrowser control?](http://stackoverflow.com/questions/153748/how-to-inject-javascript-in-webbrowser-control) – Jonathan Naguin Nov 20 '13 at 11:30
  • In my case its about Injecting external Javascript file and calling the methods inside external JS file. The suggested example or related questions shows adding scripts inline to the document.And for Microsoft.Phone.Webbrowser there is no method browser.Document – Sebastian Nov 20 '13 at 11:40
  • Check the third answer... – Jonathan Naguin Nov 20 '13 at 11:43
  • That answer is specific for Windows Webbrowser , not for Windows Phone WebBrowser , struggling again to implement that + – Sebastian Nov 21 '13 at 05:09
  • possible duplicate of [How to inject Javascript in the WP7 WebBrowser control?](http://stackoverflow.com/questions/8348503/how-to-inject-javascript-in-the-wp7-webbrowser-control) – noseratio Mar 13 '14 at 12:37

1 Answers1

1

Update: See the comments and link from Noseratio below, this only appears to work if the page on which you will InvokeScript already has a <script> tag in its head. This tag can be empty, but it must be present.

In the Windows Phone version of webbrowser, any attempt to invoke JavaScript must be done after the page has finished loading. The error you are receiving will occur if you try to invoke JS while the page is in any other state.

In your XAML, add a handler for the "LoadCompleted" event, like so:

<phone:WebBrowser IsScriptEnabled="True" LoadCompleted="DoneLoaded"  x:Name="mainBrowserControl"  Grid.Row="2"/>

Set your second button to "disabled" (Visibility.Collapsed) so that the user can't click on it. Then, In the code-behind for the page, implement the handler

    private void DoneLoaded(object sender, NavigationEventArgs e)
    {
        Debug.WriteLine("Loading Complete: "+e.Uri);
        if (e.Uri.ToString().Equals("/the_url_of_the_page_with_embedded_js.html"))
            // Enable the button that will invoke the JS
            // Or, simply perform the InvokeScript here
        }
    }

At this point, you can Navigate/NavigateToString to the page containing the JS. When it's done loading, the event above will fire and enable your "execute" button. As the page is done loading, the Invoke shouldn't puke anymore. That's assuming, of course, there isn't a bug in the JavaScript.

Hope this helps!

MojoTosh
  • 1,886
  • 1
  • 18
  • 23
  • Does this work for WP8 when the page has no ` – noseratio Mar 13 '14 at 03:08
  • 1
    It does look like a – MojoTosh Mar 13 '14 at 12:35