简体   繁体   中英

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 An unknown error has occurred. Error: 80020006 . Could you guide how to rectify this error.

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>

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); } 

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