TrN
TrN

Reputation: 1250

WebView.InvokeScript always returns empty string

I've got a problem with WebView control. I'm developing Windows 8 Metro Style app and I'm trying to invoke script in content of WebView, but it's always returns empty string.

I've tried to make it "WebView.InvokeScript("eval", new string[] { "document.getElementById('contentDiv').offsetHeight" }) as well but it works the same - empty string. I really have no guesses what wrong is here.

public void Sethtml(string html) {
            var toSet = string.Format(Style, html);
            wv.LoadCompleted += wv_LoadCompleted;
            wv.NavigateToString(toSet);

        }

void wv_LoadCompleted(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
        {
            WebView wv = sender as WebView;
            string height = wv.InvokeScript("heightFun", new string[0]); 
        }
 public static readonly string Style = @"<!DOCTYPE html>
                                               <html>
                                               <head>
                                               <title></title>
                                               <style>
                                               body {{
                                               font-family: Segoe UI; 
                                               }}
                                               </style>
                                               <script type='text/javascript'>
                                                var heightFun = function() {{ 
                                                   return document.getElementById('contentDiv').offsetHeight;
                                                }}
                                               </script>
                                               </head>

                                               <body>
                                                   <div id='contentDiv'>
                                                       {0}
                                                   </div>
                                               </body>";

Upvotes: 2

Views: 2997

Answers (2)

Geert Audenaert
Geert Audenaert

Reputation: 101

Just solved this problem via a hint of the previous answer.

Make sure your return value is a string.

Upvotes: 2

user1731468
user1731468

Reputation: 1000

You must use the window.external.notify to send a value back. See the forum post: http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/ac390a6a-d4ad-408c-8b13-93f9694cbabe

Upvotes: 2

Related Questions