none
GoBack function for webBrowser control

    Question

  • Hi,

    Not sure how to about this. It seems after loading a webPage by webBrowser control, there is no way to GOBack to the previous Webpage.

    Howt to implement a GoBack function for the webBrowser control

     

    Thanks

    Monday, January 17, 2011 12:50 AM

Answers

  • Oops, that was my fault. I was talking about the desktop control that doesn't have these events, sorry.

    The sequence of those events is: Navigating => Navigated => LoadCompleted.

    The Navigating event informs you that a navigation is about to happen and gives you the possibility to cancel it. The Navigated event is raised when the navigation is complete (cannot be canceled anymore), but the page has not yet finished loading. That is what the LoadCompleted event is for.


    Monday, January 31, 2011 5:44 AM

All replies

  • Hi. There's no built-in functionality for that, unfortunately. However, you should be able to use the LoadCompleted event and store the Source property content in a list or stack to implement your own history function. I've also seen people invoke JavaScript to navigate the history of the browser control, but that seems to mess up the internal history.


    Monday, January 17, 2011 4:11 AM
  • Hi, Thank you so much for the info.

    There are a few things I need to know about WebBrowser Controls

    1. It does not respond to any link that used to launch Ppo up Window. I had verify this one already.

    2. Does WebBrowser Support javascript?

    As for the GoBack function, I did a search and found the below. I believe this is what you mean.

    web1.NavigateToString("<html><body onLoad=\"history.go(-2);\"></body></html>");

    Your susggestion of using WebBrowser source, I have a few questions:

    a) WebBrowser control open a webpage which  has many links.

    How to I implement the stack when user click in differrent webPages?

    case 1) Click a link in Webpage_1 to go WebPage_2, in WebPage_2 click a link to go to WebPage_3.

    case 2) Click a link that can not be opened ( Like the item (1) above?) that may happen in case(1)

    Would appreciate if you can show me some code to do this task.

     

    Thanks

     

     

     

     

     

    Monday, January 17, 2011 5:13 AM
  • 1. It does not respond to any link that used to launch Ppo up Window. I had verify this one already.

    Yes; things like pop-ups, alerts and similar things are locked-down in the web browser control and don't work.

    2. Does WebBrowser Support javascript?

    Yes it does.

    case 1) Click a link in Webpage_1 to go WebPage_2, in WebPage_2 click a link to go to WebPage_3.

    You simply listen to the mentioned event, and when it is fired, check if the navigations was successful or if errors occurred. If it was successful, add the new value of the Source property to a list of URLs you maintain in your application. 

    When the user clicks a "go back" button or similar, take the Count - 1 entry of your list and navigate the web browser control to that URL. Do not store that URL again in the list in that case.

    If you don't need a "go forward" functionality, you can also use the built-in stack class instead of a list.


    Monday, January 17, 2011 7:16 AM
  • One minor thing that catches people, scripting is diabled by default so remember to set it to true on the control to make use of javascript.

    Tuesday, January 18, 2011 4:09 AM
  • Mmmm... This Back / Next stuff will only work if the links are provided by the silverlight app, if the user just navigate to another page by clicking on a link in HTML page the LoadCompleted will return NULL values.

    Seems that is a security restriction, e.g. you navigate to  another page then the SL app starts to sniff url's and send them to a service located in the server or whatever evil stuff (mmm... overkill security? this are trusted app's).

     more info:

    http://stackoverflow.com/questions/3951439/silverlight-webbrowser-source-absoluteuri-stays-at-initial-setting-even-after-a

    This will be another security feature. The user may potentially navigate anywhere, including say, their online bank. Content and location of the currently navigated page is unavailable to Silverlight. Silverlight has handed over that portion of its UI to the web browser and considers it to be outside of its security sandbox.

    I'm looking for an official Ms page where I can find that statement, what I have found so far is not very clear:

    http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser.loadcompleted.aspx

    Thanks

      Braulio

    Thursday, January 27, 2011 7:47 AM
  • Braulio, thanks for this information. That's really interesting - and unexpected, even when you think about attack scenarios.

    Thursday, January 27, 2011 12:05 PM
  • Yes, and a pain in the neck :-(, taking into account that we are talking about OOB Trusted applications (allows you to make wide range of operation using COM+)..

    Cheers

      Braulio

    Thursday, January 27, 2011 12:21 PM
  • Thank you ,Peter.

    I tried your method of getting the Url in the LoadCompleted Event. It works.

    For my case, (after reading the info on goback function from other contributor), this is what I have done for my app. Since my app required the user know the Website before hand ( specific link ), so this minimize the user from random surfing.

    1) Have a Home button that always return to home page. This is the URL enter by the user. I just captured it and store it for the home button. This will allow user to start all over again from Home page.

    private void btnHome_Click(object sender, RoutedEventArgs e)
            {
                         web1.Navigate(new Uri(g_strHomeUrl, UriKind.RelativeOrAbsolute));
               
            }

    Notice something which may be confusing: on event for webBrowser Control :

    a) What so differrent betweeen Loaded event and LoadCompleted event? 

    b) Navigated  event and Loaded event ? Which has higher priority ? which occurs first ?

    about Goback function:

    There are many conditions to take care of.  If user click goback btn while another html file is Loading , this will cause delay or longer waiting time.

    will implement the stack for the link click by user for goback function.

    Thanks

     

    Monday, January 31, 2011 4:27 AM
  • Hi again. Glad you found a sufficient solution for your scenario.

    What so differrent betweeen Loaded event and LoadCompleted event? 

    The Loaded event is not specific to the web browser, but an event of the framework element class. It is raised when the element has been created and added to the visual tree. It has nothing to do with loading a page within the web browser control as the LoadCompleted event does.

    Navigated  event and Loaded event ? Which has higher priority ? which occurs first ?

    I'm not sure I understand this. The web browser control has no Navigated event?


    Monday, January 31, 2011 4:54 AM
  • Thank for your explanation on LoadCompleted Event.

    I used these event to manipulate the progressBar to inform user the loading

     void web1_Navigating(object sender, NavigatingEventArgs e)
            {
                progressBar1.Visibility = Visibility.Visible;
            }


            void web1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
            {
               progressBar1.Visibility = Visibility.Collapsed;         
            }

    It seems they work.

    Thanks

     

     

    Monday, January 31, 2011 5:31 AM
  • Oops, that was my fault. I was talking about the desktop control that doesn't have these events, sorry.

    The sequence of those events is: Navigating => Navigated => LoadCompleted.

    The Navigating event informs you that a navigation is about to happen and gives you the possibility to cancel it. The Navigated event is raised when the navigation is complete (cannot be canceled anymore), but the page has not yet finished loading. That is what the LoadCompleted event is for.


    Monday, January 31, 2011 5:44 AM