Reputation: 823
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 = \"[email protected]\";" +
" }" +
"//]]>" +
"</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.?
Upvotes: 0
Views: 3319
Reputation: 823
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.
Upvotes: 1
Reputation: 1516
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);
});
Upvotes: 1