Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bryanne Skolaski 10 posts 80 karma points
    Jun 16, 2017 @ 15:17
    Bryanne Skolaski
    0

    Using anchor links in nav menu url issues

    I have a new site where part of my navigation is anchor tags on the home page, and part is child pages. I can't seem to figure out how to get the URL to the root from the child pages, so that it directs back to the home page/anchor.

    I'm using razor and umbraco 7.6

    Thanks!

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jun 16, 2017 @ 15:49
    Alex Skrypnyk
    0

    Hi Bryanne

    Did you mean url part after "#" as anchor?

    Umbraco doesn't handle this part of url, it's client side totally. You have to use some js script for doing it.

    Thanks,

    Alex

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jun 16, 2017 @ 15:53
    Nik
    0

    Hi Bryanne,

    In your code that generates the menu, you should be able to use something like:

    @Model.Content.Site.Url
    

    This should get the root node of your site so you can append it your anchor entries when creating the menu.

    Alternatively, you could just append "/" before your anchors when you create the menu.

    :-)

    Nik

  • Bryanne Skolaski 10 posts 80 karma points
    Jun 16, 2017 @ 17:19
    Bryanne Skolaski
    0

    the @Model.Content.Site.Url throws this error:'Umbraco.Web.PublishedContentExtensions.Site(Umbraco.Core.Models.IPublishedContent)' is a 'method', which is not valid in the given context

    and, using

    <a href="/#opportunity">Opportunity</a>
    

    works fine on the root page, but on a child page it doesn't navigate it back to the root page, it just sits there.

  • Amir Khan 1282 posts 2739 karma points
    Jun 16, 2017 @ 17:30
    Amir Khan
    0

    Hi Bryanne,

    You can also do it like this:

    <a href="@Umbraco.TypedContentAtRoot().First().Url#opportunity"> Opportunity </a>
    

    Hope this helps!

    Amir

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Jun 16, 2017 @ 17:34
    Nicholas Westby
    0

    Is there an error in the Chrome developer console when you click the link?

    Are you using AngularJS or Angular?

  • Bryanne Skolaski 10 posts 80 karma points
    Jun 16, 2017 @ 17:48
    Bryanne Skolaski
    0

    There are no errors. Im using Razor/mvc, no angular etc.

    When using

    <a href="@Umbraco.TypedContentAtRoot().First().Url#opportunity"> Opportunity </a>
    

    i get this HTML output

    <a href="/#opportunity">Opportunity</a>
    

    Which works fine on the home page, but from a child page it still doesnt' navigate anywhere

    Im working in a local dev environment if that matters

  • Amir Khan 1282 posts 2739 karma points
    Jun 16, 2017 @ 17:51
    Amir Khan
    0

    That's really bizarre...are your child pages underneath your homepage? Mind uploading a screenshot of your content tree?

  • Bryanne Skolaski 10 posts 80 karma points
    Jun 16, 2017 @ 17:54
    Bryanne Skolaski
    0

    This is my content tree.

    enter image description here

    This is my menu code:

     @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
     @using umbraco.MacroEngines;
    
     @{ var home = CurrentPage.Site(); }
     @if (home.Children.Any())
     {
    @* Get the first page in the children *@
     var naviLevel = home.Children.First().Level;
    
    @* Add in level for a CSS hook *@
    <ul id="js-navigation-menu" class="navigation-menu">
    
    
        @*Static anchor links with no pages*@
        <li><span><a href="@Umbraco.TypedContentAtRoot().First().Url#opportunity" class="anchor-link">Opportunity</a></span></li>
        <li><span><a href="@Umbraco.TypedContentAtRoot().First().Url#empowering-philanthropy" class="anchor-link">Empowering Philanthropy</a></span></li>
        <li><span><a href="@Umbraco.TypedContentAtRoot().First().Url#who-we-are" class="anchor-link">Who we are</a></span></li>
        @* For each child page under the home node *@
        @foreach (var childPage in home.Children.Where("umbracoNaviHide != true"))
        {
            if (childPage.Children.Any())
            {
                <li class="has-child">
                    @if (childPage.DocumentTypeAlias == "LandingPage")
                    {
                        <span><a href="@childPage.Url" class="@(childPage.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">@childPage.Name <i class="fa fa-angle-down"></i></a></span>
                        @childPages(childPage.Children)
                    }
                    else
                    {
                            <span><a href="@childPage.Url" class="@(childPage.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">@childPage.Name</a></span>
                    }
                </li>
            }
            else
            {
                <li>
                    <span><a href="@childPage.Url" class="@(childPage.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">@childPage.Name</a></span>
                </li>
            }
        }
    </ul>
     }
     @helper childPages(dynamic pages)
     {
    @* Ensure that we have a collection of pages *@
     if (pages.Any())
     {
    @* Get the first page in pages and get the level *@
     var naviLevel = pages.First().Level;
    
    @* Add in level for a CSS hook *@
    <ul class="submenu">
        @foreach (var page in pages)
        {
            <li>
                <a href="@page.Url" class="@(page.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">@page.Name</a>
    
            </li>
        }
    </ul>
     }
     }
    
  • Bryanne Skolaski 10 posts 80 karma points
    Jun 16, 2017 @ 17:56
    Bryanne Skolaski
    0

    and this is how the HTML renders

    <nav role="navigation" id="nav" itemscope itemtype="http://schema.org/SiteNavigationElement">
    
    <ul id="js-navigation-menu" class="navigation-menu">
    
        <li><span><a href="/#opportunity" class="anchor-link">Opportunity</a></span></li>
        <li><span><a href="/#empowering-philanthropy" class="anchor-link">Empowering Philanthropy</a></span></li>
        <li><span><a href="/#who-we-are" class="anchor-link">Who we are</a></span></li>
        <li><span><a href="/news/">News</a></span></li>
        <li><span><a href="/contact-us/">Contact Us</a></span></li>
    </ul>
    
    </nav>
    
  • Amir Khan 1282 posts 2739 karma points
    Jun 16, 2017 @ 18:00
    Amir Khan
    0

    That looks normal and I just tried the code for your nav and it worked fine for me.

    Are you sure there isn't something with the JS for your menu that's catching the anchor links and preventing navigation?

    As a test, you may want to just remove that javascript and see if navigation occurs as expected.

    -Amir

  • Bryanne Skolaski 10 posts 80 karma points
    Jun 16, 2017 @ 18:10
    Bryanne Skolaski
    0

    I was actually just investigating the JS myself.

    Seems there's something in here thats breaking it.

    $(document).ready(function () {
    var menuToggle = $('#js-mobile-menu').unbind();
    
    menuToggle.on('click', function (e) {
        e.preventDefault();
        $('#js-navigation-menu').toggleClass("mobile-anchors");
        $('#js-navigation-menu').slideToggle(function () {
            if ($('#js-navigation-menu').is(':hidden')) {
                $('#js-navigation-menu').removeAttr('style');
            }
        });
    });
    
    var anchorToggle = $('.anchor-link').unbind();
    
    anchorToggle.on('click', function (e) {
        e.preventDefault();
    
        $('a').each(function () {
            $(this).removeClass('selected');
        })
        $(this).addClass('selected');
    
        $('.mobile-anchors').slideToggle(function () {
            if ($('.mobile-anchors').is(':hidden')) {
                $('.mobile-anchors').removeAttr('style');
                $('#js-navigation-menu').removeClass("mobile-anchors");
            }
        });
    });
    });
    

    What this is, is a function to open my mobile menu, and then, when clicking the anchor links, to close said mobile menu.

  • Amir Khan 1282 posts 2739 karma points
    Jun 16, 2017 @ 18:15
    Amir Khan
    1

    Ahhh. So this "e.preventDefault();" prevents the default action (navigating to the target) of the anchor tag from occurring :)

    You'll likely want to rework your js logic.

    -Amir

  • Bryanne Skolaski 10 posts 80 karma points
    Jun 16, 2017 @ 18:18
    Bryanne Skolaski
    0

    Yup. Unfortunately i'm not a JS guru, so might take me some time. If you have any tips that would be great!

  • Amir Khan 1282 posts 2739 karma points
    Jun 16, 2017 @ 18:23
    Amir Khan
    0

    You may want to look at some of the menu plugins that are out there if you want to make it easy on yourself. We use this one quite often: https://github.com/vadikom/smartmenus

    Also, I'm not sure why, if you're wanting to navigate to a new page, you need to close the menu? Wouldn't it just be closed when you land on the new page?

    Hope this helps,

    Amir

  • Bryanne Skolaski 10 posts 80 karma points
    Jun 16, 2017 @ 18:42
    Bryanne Skolaski
    0

    There's 3 links that are anchors, so those need to close the menu, and there's 2 that are actual pages.

  • Amir Khan 1282 posts 2739 karma points
    Jun 16, 2017 @ 18:56
    Amir Khan
    0

    General guidance would be to add a class to the toggle links only and only apply your preventdefaut method / toggle logic to those.

    Away from my desk or would write something up for you.

  • Bryanne Skolaski 10 posts 80 karma points
    Jun 16, 2017 @ 19:07
    Bryanne Skolaski
    0

    Thanks! working that thru now

Please Sign in or register to post replies

Write your reply to:

Draft