Reputation: 35
I have some difficulties with the method InvokeScript
on wp7:
webBrowser1.InvokeScript("eval", string.Format("document.getElementsByName('email').value='{0}'", _email));
webBrowser1.InvokeScript("eval", string.Format("document.getElementsByName('pass').value='{0}'", _pass));
webBrowser1.InvokeScript("eval", "document.forms[0].submit();");
Unfortunately, when I try to submit information, using (document.forms[0].submit())
, an exception is thrown with the message:
An unknown error has occurred. Error: 80020101.
What may the problem be?
Upvotes: 2
Views: 2434
Reputation: 856
First make sure that IsScriptingEnabled is to true, but I assume you did it.
Your problem is probably that you call the code too soon. It seems that the DOM is not ready for manipulation when the Navigated event occurs. Example:
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
Wb.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(Wb_Navigated);
MouseLeftButtonDown += new MouseButtonEventHandler(MainPage_MouseLeftButtonDown);
Wb.NavigateToString("<html><body><form action='http://google.com/'></form></body></html>");
}
void Wb_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
Wb.InvokeScript("eval", "document.forms[0].submit();"); // Throws 80020101
}
private void MainPage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Wb.InvokeScript("eval", "document.forms[0].submit();"); // Works
}
}
Upvotes: 3
Reputation: 4849
Script can directly submit forms - I have an app that depends on that behavior. :)
The problem is that when there's no script on the page (and in some cases, when there's only a little script - I have not been able to isolate this), the browser does not load (or maybe loads and unloads) the script engine. When that happens, you simply cannot execute any script.
One thing that worked for me on a specific case was to make a local copy of the HTML, add random script to it, and load it as string in the browser, but that obviously will not work for everything.
Another alternative (though much more work) for me in another case was to use the HtmlAgilityPack to read the HTML and do the post myself.
Upvotes: 0