locked
Navigation to Fragment RRS feed

  • Question

  • i see that at the moment there is no support in the navigation service for fragments.
    WHat about navigation in the WebBrowser?
    For example, if i NavigateToString() with some HTML code which works. and the code has bookmarks defined and the browser is able to jump to the bookmarks.
    is there a way to programatically make the jumps to the bookmarks?
    Sunday, December 26, 2010 9:44 AM

Answers

  • actually there is a way to do this.
    create anchors in the html code
    create a javascriipt function that calls location.Replace(#newLocation) where new location is the id of the anchor
    call WebBrowser.InvokeScript("JSfunction", "#newLocation");
    this will reposition the web browser to the new location
    Wednesday, January 12, 2011 7:20 AM

All replies

  • Hi Biconix,

    I saw some mention that there might have been some updates in the November tools update, but I can't say specifically if Fragement navigation support was added at that time or not.  Previously it was unsupported.  You might just try running a quick sample, but don't be surprised if you get an exception that looks like this:

    {System.Windows.Navigation.NavigationFailedEventArgs}

        base {System.EventArgs}: {System.Windows.Navigation.NavigationFailedEventArgs}

        Exception: {"No Fragment support right now"}

        Handled: false

        Uri: {/CommentsPage.xaml?uri=http://www.reddit.com/r/funny/comments/cpt8b/if_you_watch_a_horror_movie_backwards/}


      Thanks,

    ~Jonathan Tanner [MSFT]

    Tuesday, December 28, 2010 12:57 AM
  • actually there is a way to do this.
    create anchors in the html code
    create a javascriipt function that calls location.Replace(#newLocation) where new location is the id of the anchor
    call WebBrowser.InvokeScript("JSfunction", "#newLocation");
    this will reposition the web browser to the new location
    Wednesday, January 12, 2011 7:20 AM
  • How exactly? I have tried this by hooking into the onNavigated event but it doesn't work.

    In my page:

    function fragNav(frag) {
        window.location.replace(frag);
    }

    and in the C#,

            private void webBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
            {
                if (this.NavStack.Count > 0)
                {
                    if (this.NavStack.Peek() == e.Uri) return;
                }
                this.NavStack.Push(e.Uri);
                string[] els = e.Uri.ToString().Split(new char[] { '#' });
                if (els.Length > 1)
                {
                    string[] param = new string[] { "#" + els[els.Length - 1] };
                    this.webBrowser.InvokeScript("fragNav", param);
    
                }
            }

    (the 'NavStack' is a C# stack used for navigation, but in this case is necessary to stop infinite loops). I have also tried out a similar solution in Javascript but it also does not work. Weirdly if you call window.location.replace after attaching to an element click event it does work.

    This is driving me nuts and is the only thing holding back the publishing of my app!

    Friday, November 23, 2012 12:22 AM
  • You might try inserting a fragment in your document like the following:

    function showIt(elID) {
    var el = document.getElementById(elID);
    el.scrollIntoView();
    }

    Then in the LoadCompleted event calling it like this:

    browser1.InvokeScript("showIt", NavigateToFragment.Replace("#", ""))

    In the Navigating event you can capture what the fragment is and then save it to a variable like NavigateToFragement above. Then after you call the 'showIt' function, set the string to String.Empty again.
    Monday, July 29, 2013 3:36 PM