Navigating with the WebBrowser Control on WP7

GooNews

I am writing a new Windows Phone 7 application called GooNews to show Google News in an application for the phone.  I am writing this application because I needed an app like this. Being able to keep up with news (and create news categories based on keywords) is key to what I wanted to get on my phone. I tried a lot of the other apps out there and when I didn't find what I wanted, I decide to build it myself.

In writing GooNews, I needed to support refresh, back and forward for the web page shown in the WebBrowser control. In looking at the API for the WebBrowser control I noticed quickly that it wasn't suppored on the control.  So a quick search revealed a good solution: use the InvokeScript call to tell the page to perform these actions:

private void refreshButton_Click(object sender, EventArgs e)
{
  browser.InvokeScript("eval", "history.go()");
}

But when I ran this code, I kept getting obscure errors (actually, probably COM exception codes, e.g. 80004001). I couldn't figure out what was going on but then I found the solution:

<phone:WebBrowser x:Name="browser"
                  IsScriptEnabled="True" />

Without enabling scripting, invoking script doesn't work. Its obvious but hard to discover since the error messages are obscure.  Because the operations may fail if the page has handled certain types of scripts or has a function called eval, you should trap errors in the application.  Here's what my final Back, Refresh and Forward button functionality look like:

private void backButton_Click(object sender, EventArgs e)
{
  try
  {
    browser.InvokeScript("eval", "history.go(-1)");
  }
  catch
  {
    // Eat error
  }
}

private void refreshButton_Click(object sender, EventArgs e)
{
  try
  {
    browser.InvokeScript("eval", "history.go()");
  }
  catch
  {
    // Eat error
  }
}

private void forwardButton_Click(object sender, EventArgs e)
{
  try
  {
    browser.InvokeScript("eval", "history.go(1)");
  }
  catch
  {
    // Eat error
  }
}

I hope this helps you've run into this problem!

 

 

Comments

Gravatar

Keith Sunday, December 26, 2010

Thanks Shawn, I'm trying this do you happen to know how to do thi in the Silverlight 4 OOB version? I set OOB properties to require elevated trust, but when I try:

this.Browser1.InvokeScript("eval", "history.go(-1);");

I get a SecurityException: Attempted to perform an unauthorized operation.

Thanks in advance if your or anyone else reading has any ideas.

- Keith

Gravatar

Shawn Wildermuth Monday, December 27, 2010

Is the page your on hosted from a separate domain? If so, you're not allowed to do it.

Gravatar

Chris Eargle Monday, December 27, 2010

This was on my list of features for my own apps. Thanks!

Gravatar

Keith Monday, December 27, 2010

Yup, that's it. I think we'll end up doing WPF instead. Thanks!

Gravatar

Qi Tuesday, December 28, 2010

Hello Shawn:

I got a weird exception when I tried to refresh the page (I have set the IsScriptEnabled to true):

============================
An unhandled exception of type 'System.SystemException' occurred in Microsoft.Phone.Interop.dll

Additional information: An unknown error has occurred. Error: 80020006.
============================

This is my code:

webBrowser.InvokeScript("eval", "history.go()");

Could you give me some advises about the problem? Thanks a lot!

Gravatar

za Saturday, February 26, 2011

Sir, you are great :)

Gravatar

bill Saturday, June 25, 2011

I get exceptions on the phone, but works OK in the emulator. Any ideas?

IsScriptEnabled is set to true; again, works in the emulator but not on the phone. Was something 'updated' in 7390?

Gravatar

JYA Monday, October 24, 2011

I too get a SystemException was unhandled error (error 80020006).
Page is local (running from IsolatedStorageFile)

I read about setting the IsScriptEnabled property prior to loading the content : but this made no difference.
The code provided in this example is included in a try .. catch so you probably won't see that calling the javascript just doesn't work as the exception is hidden

Gravatar

Jean-Yves Avenard Monday, October 24, 2011

According to a Microsoft representative, you can’t reference a JavaScript function directly; hence the error 80020006

Gravatar

Arjun Saturday, December 08, 2012

How can I handle going back to the previous page, but with the view being at the place where he left from. (Like he scroll down for a minute, clicks a link that takes him to a new page. When he clicks back, it should send him to the previous page, and the web browser control should display the place where he left it from and not from the very top of the previous page)

Gravatar

grant Sunday, December 09, 2012

Very great tips!
For Qi's question, it may be caused by the content of the HTML file. You can try another HTML files.

Here is a very detailed answers for the 80020006 error.
http://cncc.bingj.com/cache.aspx?q=80020006&d=4598978802418791&mkt=zh-CN&setlang=en-US&w=w5DCjZJ9ZmcrX7Y6hza8PCv-YpVbGBy5

Gravatar

raj Saturday, April 13, 2013

thanks so much, i needed this sooo much

Gravatar

Koja Monday, September 09, 2013

Thanks, nice article!


Leave a Comment

*
*
*