user2189143
user2189143

Reputation:

C# InvokeScript gives error 80020006

I have the piece of code as follows.

webBrowser.IsScriptEnabled = true;
webBrowser.InvokeScript("eval", "alert('hey')");

It gives An unknown error has occurred. Error: 80020006. Could you guide how to rectify this error.

Upvotes: 2

Views: 3415

Answers (2)

Vinh Vo
Vinh Vo

Reputation: 171

It is caused by race condition. What we need to do is

this.CordovaView.Browser.LoadCompleted += Browser_LoadCompleted;

then

  void Browser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        this.CordovaView.Browser.IsScriptEnabled = true;
        this.CordovaView.CordovaBrowser.IsScriptEnabled = true;
        this.CordovaView.Browser.InvokeScript("setPushToken", push_uri);
    }

Upvotes: 2

Cybermaxs
Cybermaxs

Reputation: 24558

There is no built-in browser window.alert in Windows Phone, but you can bind one as follows to call WebBrowser.ScriptNotify

//inside the page
window.alert = function (__msg) { window.external.notify(' + __msg + '); };


// in your C# code
this.webBrowser.ScriptNotify += new EventHandler<NotifyEventArgs>(Browser_ScriptNotify);
void Browser_ScriptNotify(object sender, NotifyEventArgs e)
{
     MessageBox.Show(e.Value);
}

//later 
this.CordovaView.Browser.IsScriptEnabled = true;
this.CordovaView.Browser.InvokeScript("alert", "ok");

On Cordova, there is also a Notification Plugin that you can plug by

window.alert = navigator.notification.alert;

Just be sure to enable Notification Plugin in config.xml

  <feature name="Notification">
      <param name="wp-package" value="Notification"/>
  </feature>

Upvotes: 2

Related Questions