简体   繁体   中英

WP7 Webbrowser InvokeScript errors

WebBrowser control's invoke script is always giving me errors. This html script is validated from http://validator.w3.org . I wrote the code such that on clicking "button1" webBrowser1 invokes the function "setCredentials". I am not sure why this is giving an error like

"An unknown error has occurred. Error: 80020006."

    public TestInvokeScript()
    {
        InitializeComponent();
        LoadHtml();
        webBrowser1.IsScriptEnabled = true;
        webBrowser1.NavigateToString(_html);

        button1.Content = "Set Credentials";
    }

    private void LoadHtml()
    {

        _html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"" +
                "    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
                "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">" +
                "<head>" + "<meta name=\"generator\" content=\"HTML Tidy for Linux (vers 6 November 2007), see www.w3.org\" />" +
                "<script type=\"text/javascript\">" +
                "//<![CDATA[" +
                "            function setCredentials()" +
                "                        {" +
                "                                document.getElementById(\"email\").value = \"test@gmail.com\";" +
                "                        }" +
                "//]]>" +
                "</script>" +
                "<title></title>" +
                "</head>" +
                "<body>" +
                "<form action=\"https://cloudmagic.com/k/login/send\" method=\"post\">" +
                "<input id=\"email\" type=\"text\" value=\"\" /> " +
                "<input id=\"password\" type=\"password\" />" +
                " <button type=\"submit\" id=\"login_send\">Login</button>" +
                " </form>" +
                "</body>" +
                "</html>";
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
         var obj = webBrowser1.InvokeScript("setCredentials");             
    }

What is the mistake I am doing.?

A couple of possibilities:
1. Make sure you call it after PageLoaded or NavigateComplete fires.
2. Try this one:

Dispatcher.BeginInvoke(() =>
                  {

                      var result = webBrowser.InvokeScript("javascrpitMethod", param1, param2);
                  });

I was passing the html in string form. Unknowingly, I used a double slash(//) and that commented the rest of the string since there are no newLine characters. It took me almost a day to figure this out. Remove the double slashes and CData tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM