简体   繁体   中英

Go Back in WebBrowser

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...?

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.

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