0

I can't find any doucmentation on this. I have a WebBrowser control for my C# Windows Phone app, and I would like to have a button to take the user back one page. How can I do this? (I know how to make the button or access the physical back button, the question is just about how to make a function to take the user back one page.

Thanks.

EDIT: Also, if there's nothing to go back to, I'd like to run another function if possible.

EDIT: Tried this: browser.InvokeScript("eval", "history.go(-1)");, it's not working for some reason. It just closes the app.

EDIT: Got the above working with e.Cancel = true; but how do I restore the default function if there is no page to go back to...?

JacobTheDev
  • 17,318
  • 25
  • 95
  • 158
  • possible duplicate of [Handling hardware back button and sending it to WebBrowser control running on Windows Phone](http://stackoverflow.com/questions/5100538/handling-hardware-back-button-and-sending-it-to-webbrowser-control-running-on-wi) – BrokenGlass Nov 13 '11 at 21:42
  • Kind of a duplicate but kind of not. That link helped me but there's still a question to be answered. – JacobTheDev Nov 13 '11 at 22:30

1 Answers1

1

There are a couple of suggestions in this thread that may work, especially since you don't have to worry about multiple browsers. One way is to use the `history.length' property:

if(history.length > 0)
{
  //go back
}
else
{
  //cannot go back so perform other function
}

Another way suggested in that thread is to check the referrer. The first page doesn't usually have a referrer:

if (document.referrer == "") {
    window.close()
} else {
    history.back()
}

Invoke these scripts the same way you invoked the history.go() script and see if that works.

Community
  • 1
  • 1
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • Apparently I've asked this question before. So is this JavaScript or C# then? Looks like JavaScript, I'll have to think of a way to do this... – JacobTheDev Dec 23 '11 at 02:46
  • The problem I have with this is that it answers the question the opposite way. I need it to check in C# basically...It's a little confusing, but I basically need to figure out how to reverse it... – JacobTheDev Dec 23 '11 at 02:51