Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "manually" go back with a WebBrowser?

I'm working on a web scraper that sometimes needs to remember a particular page, then go to some other pages and then go back to that page. Currently I just save the URL of the page, but that doesn't work for pages like Google Maps, where the URL is always the same.

I can see that the GoBack method does go back to the previous page, so somehow the WebBrowser remembers what the previous pages was. How can I do this manually? I could count how many pages have been visited since the page I want to go back to and then call GoBack as many times as necessary, but that's pretty unreliable and un-elegant. So I wonder how could I implement a GoBackToAParticularPage method.

There is one thing I think would get me closer to a solution: saving the URL of all frames and then putting them back when going back to that page. I think that would solve at lease the Google Maps problems. I have not tested it yet. I don't know exactly what would it be the proper way to do this. I would need to wait for the frames to exist before setting their URLs.

like image 867
Juan Avatar asked Mar 24 '11 05:03

Juan


5 Answers

You can use

webBrowser1.Document.Window.History.Go(x);

where x is an int signifying the relative position in the browser's history.

x=-2 would navigate two pages back.

Update: More info on HtmlHistory.Go()

like image 151
Yetti Avatar answered Oct 22 '22 15:10

Yetti


try this!

javascript:history.go(-1)"

like image 4
Renato Gama Avatar answered Oct 22 '22 16:10

Renato Gama


I know a few things have been said, so i won't re-write that, however, if you really want to use a JavaScript method (ie: if you want to use the javascript history object instead of the webbrowser controls history object) and are wondering how, there are ways to do this. You can use .InvokeScript in .NET WB controls, or if you want pre-.NET & .NET compatible, you can use this:

You can use .execScript in pre-.NET versions of WB control and current/.NET versions of WB control. You can also choose the language of the script you want to execute, ie: "JScript" or "VBScript". Here is the one liner:

WebBrowser1.Document.parentWindow.execScript "alert('hello world');", "JScript" 

The good thing about using the JavaScript history object is that if you kill history information in the webbrowser control by sending the number "2" into the .navigate method, going to the page where history was cancelled in WB control will not work, but it will work in the JavaScript's history object, this is an advantage.

Once again, this is just a backwards compatible supplement to the ideas discussed on this post already, including a few other tidbits not mentioned.

Let me know if i can be of further help to you since and answer was already accepted.

like image 2
Erx_VB.NExT.Coder Avatar answered Oct 22 '22 16:10

Erx_VB.NExT.Coder


By javascript Location object you may achieve you task.

<FORM><INPUT TYPE="BUTTON" VALUE="Go Back" 
ONCLICK="history.go(-1)"></FORM>

also check

JavaScript History Object

for the history information

like image 1
Pranay Rana Avatar answered Oct 22 '22 14:10

Pranay Rana


Browser history, by design, is opaque; otherwise it opens a security hole: Do you really want every page you visit to have visibility as to what pages/sites you've been visiting? Probably not.

To do what you want, you'll need to implement your own stack of URIs, tracking what needs to be revisited.

like image 1
Nicholas Carey Avatar answered Oct 22 '22 15:10

Nicholas Carey