How to add Javascript to a WP7 WebBrowser control?

I can add JavaScript to the WebBrowser control in the form of a C # window at this link

How to add JavaScript to a WebBrowser control?

But I can not do this in WP7, please help me.

+5
source share
2 answers

Unfortunately, WebBrowser.Documentnot available in WP7. But you can create and call a JavaScript function using InvokeScript. Look here where I describe how.

: .Document #, JavaScript. eval script as . :

webBrowser1.InvokeScript("eval", "  ...code goes here... ");
+7

WebBrowser (WinForms/WPF) - InvokeScript("eval", ...) <script>. I.e., JavaScript (, <body></body>), eval .

Windows Phone SDK/emulator, , Windows Phone WebBrowser.

, Windows Store App. , this.webBrowser.InvokeScript("setTimeout", ...), JavaScript. execScript, IE11.

async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    // load a blank page
    var tcsLoad = new TaskCompletionSource<object>();
    this.webBrowser.NavigationCompleted += (s, eArgs) =>
        tcsLoad.TrySetResult(Type.Missing);
    this.webBrowser.NavigateToString("<body></body>");
    await tcsLoad.Task;

    // first add a script via "setTimeout", JavaScript gets initialized
    var tcsInit = new TaskCompletionSource<object>();
    this.webBrowser.ScriptNotify += (s, eArgs) =>
    {
        if (eArgs.Value == "initialized")
            tcsInit.TrySetResult(Type.Missing);
    };
    this.webBrowser.InvokeScript("setTimeout", 
        new string[] { "window.external.notify('initialized')", "0" });
    await tcsInit.Task;

    // then use "eval"
    this.webBrowser.InvokeScript("eval", 
        new string[] { "document.body.style.backgroundColor = 'yellow'" });
}

, - , WP WebBrowser . LoadCompleted WP NavigationCompleted WebBrowser.IsScriptEnabled true.

+1

All Articles