Change active navbar link when using JQUERY Load











up vote
0
down vote

favorite












I have a navigation bar which is being loaded into each of my pages using a JQUERY '.load'



However, the active link will be different for each page. Is there a way i can override the active link from the page that has loaded the Navbar?



Any help would be much appreciated :)



Below is an example of the Navbar links



  <nav class="navbar navbar-toggleable-sm">
<button type="button" data-toggle="collapse" data-target="#mai-navbar-collapse" aria-controls="#mai-navbar-collapse" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler hidden-md-up collapsed">
<div class="icon-bar"><span></span><span></span><span></span></div>
</button>
<div id="mai-navbar-collapse" class="navbar-collapse collapse mai-nav-tabs">
<ul class="nav navbar-nav">
<li class="nav-item parent open"><a href="#" role="button" aria-expanded="true" class="nav-link"><span class="icon s7-home"></span><span>Training</span></a>
<ul class="mai-nav-tabs-sub mai-sub-nav nav">
<li class="nav-item"><a href="/teams/Training/sitepages/training/writtenguides.aspx" id="12222" class="nav-link active"><span class="icon s7-monitor"></span><span class="name">Written Guides</span></a></li>
<li class="nav-item"><a href="/teams/Training/sitepages/training/videoguides.aspx" class="nav-link"><span class="icon s7-video"></span><span class="name">Video Guides</span></a></li>
</ul>
</li>
<li class="nav-item parent"><a href="#" role="button" aria-expanded="false" class="nav-link"><span class="icon s7-users"></span><span>Support</span></a>
<ul class="mai-nav-tabs-sub mai-sub-nav nav">
<li class="nav-item"><a href="/teams/Training/sitepages/support/signup.aspx" class="nav-link"><span class="icon s7-id"></span><span class="name">Signing Up</span></a></li>
<li class="nav-item"><a href="/teams/Training/sitepages/support/faqs.aspx" class="nav-link"><span class="icon s7-search"></span><span class="name">FAQS</span></a></li>
</ul>
</li>

</ul>
</div>
</nav>


JQUERY



 <script>
$(function(){
$("#nav-placeholder").load("../../navbarL1.aspx");
$('.dropdown-toggle').dropdown()
});



</script>









share|improve this question
























  • You can check the url where you are currently, and make it active depending on this(/faqs, /signup,..)..for example
    – Albeis
    Mar 5 at 14:45










  • Include the relevant jQuery code too.
    – Zim
    Mar 5 at 15:36










  • @ZimSystem code updated with JQUERY added
    – Nick
    Mar 5 at 16:15















up vote
0
down vote

favorite












I have a navigation bar which is being loaded into each of my pages using a JQUERY '.load'



However, the active link will be different for each page. Is there a way i can override the active link from the page that has loaded the Navbar?



Any help would be much appreciated :)



Below is an example of the Navbar links



  <nav class="navbar navbar-toggleable-sm">
<button type="button" data-toggle="collapse" data-target="#mai-navbar-collapse" aria-controls="#mai-navbar-collapse" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler hidden-md-up collapsed">
<div class="icon-bar"><span></span><span></span><span></span></div>
</button>
<div id="mai-navbar-collapse" class="navbar-collapse collapse mai-nav-tabs">
<ul class="nav navbar-nav">
<li class="nav-item parent open"><a href="#" role="button" aria-expanded="true" class="nav-link"><span class="icon s7-home"></span><span>Training</span></a>
<ul class="mai-nav-tabs-sub mai-sub-nav nav">
<li class="nav-item"><a href="/teams/Training/sitepages/training/writtenguides.aspx" id="12222" class="nav-link active"><span class="icon s7-monitor"></span><span class="name">Written Guides</span></a></li>
<li class="nav-item"><a href="/teams/Training/sitepages/training/videoguides.aspx" class="nav-link"><span class="icon s7-video"></span><span class="name">Video Guides</span></a></li>
</ul>
</li>
<li class="nav-item parent"><a href="#" role="button" aria-expanded="false" class="nav-link"><span class="icon s7-users"></span><span>Support</span></a>
<ul class="mai-nav-tabs-sub mai-sub-nav nav">
<li class="nav-item"><a href="/teams/Training/sitepages/support/signup.aspx" class="nav-link"><span class="icon s7-id"></span><span class="name">Signing Up</span></a></li>
<li class="nav-item"><a href="/teams/Training/sitepages/support/faqs.aspx" class="nav-link"><span class="icon s7-search"></span><span class="name">FAQS</span></a></li>
</ul>
</li>

</ul>
</div>
</nav>


JQUERY



 <script>
$(function(){
$("#nav-placeholder").load("../../navbarL1.aspx");
$('.dropdown-toggle').dropdown()
});



</script>









share|improve this question
























  • You can check the url where you are currently, and make it active depending on this(/faqs, /signup,..)..for example
    – Albeis
    Mar 5 at 14:45










  • Include the relevant jQuery code too.
    – Zim
    Mar 5 at 15:36










  • @ZimSystem code updated with JQUERY added
    – Nick
    Mar 5 at 16:15













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a navigation bar which is being loaded into each of my pages using a JQUERY '.load'



However, the active link will be different for each page. Is there a way i can override the active link from the page that has loaded the Navbar?



Any help would be much appreciated :)



Below is an example of the Navbar links



  <nav class="navbar navbar-toggleable-sm">
<button type="button" data-toggle="collapse" data-target="#mai-navbar-collapse" aria-controls="#mai-navbar-collapse" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler hidden-md-up collapsed">
<div class="icon-bar"><span></span><span></span><span></span></div>
</button>
<div id="mai-navbar-collapse" class="navbar-collapse collapse mai-nav-tabs">
<ul class="nav navbar-nav">
<li class="nav-item parent open"><a href="#" role="button" aria-expanded="true" class="nav-link"><span class="icon s7-home"></span><span>Training</span></a>
<ul class="mai-nav-tabs-sub mai-sub-nav nav">
<li class="nav-item"><a href="/teams/Training/sitepages/training/writtenguides.aspx" id="12222" class="nav-link active"><span class="icon s7-monitor"></span><span class="name">Written Guides</span></a></li>
<li class="nav-item"><a href="/teams/Training/sitepages/training/videoguides.aspx" class="nav-link"><span class="icon s7-video"></span><span class="name">Video Guides</span></a></li>
</ul>
</li>
<li class="nav-item parent"><a href="#" role="button" aria-expanded="false" class="nav-link"><span class="icon s7-users"></span><span>Support</span></a>
<ul class="mai-nav-tabs-sub mai-sub-nav nav">
<li class="nav-item"><a href="/teams/Training/sitepages/support/signup.aspx" class="nav-link"><span class="icon s7-id"></span><span class="name">Signing Up</span></a></li>
<li class="nav-item"><a href="/teams/Training/sitepages/support/faqs.aspx" class="nav-link"><span class="icon s7-search"></span><span class="name">FAQS</span></a></li>
</ul>
</li>

</ul>
</div>
</nav>


JQUERY



 <script>
$(function(){
$("#nav-placeholder").load("../../navbarL1.aspx");
$('.dropdown-toggle').dropdown()
});



</script>









share|improve this question















I have a navigation bar which is being loaded into each of my pages using a JQUERY '.load'



However, the active link will be different for each page. Is there a way i can override the active link from the page that has loaded the Navbar?



Any help would be much appreciated :)



Below is an example of the Navbar links



  <nav class="navbar navbar-toggleable-sm">
<button type="button" data-toggle="collapse" data-target="#mai-navbar-collapse" aria-controls="#mai-navbar-collapse" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler hidden-md-up collapsed">
<div class="icon-bar"><span></span><span></span><span></span></div>
</button>
<div id="mai-navbar-collapse" class="navbar-collapse collapse mai-nav-tabs">
<ul class="nav navbar-nav">
<li class="nav-item parent open"><a href="#" role="button" aria-expanded="true" class="nav-link"><span class="icon s7-home"></span><span>Training</span></a>
<ul class="mai-nav-tabs-sub mai-sub-nav nav">
<li class="nav-item"><a href="/teams/Training/sitepages/training/writtenguides.aspx" id="12222" class="nav-link active"><span class="icon s7-monitor"></span><span class="name">Written Guides</span></a></li>
<li class="nav-item"><a href="/teams/Training/sitepages/training/videoguides.aspx" class="nav-link"><span class="icon s7-video"></span><span class="name">Video Guides</span></a></li>
</ul>
</li>
<li class="nav-item parent"><a href="#" role="button" aria-expanded="false" class="nav-link"><span class="icon s7-users"></span><span>Support</span></a>
<ul class="mai-nav-tabs-sub mai-sub-nav nav">
<li class="nav-item"><a href="/teams/Training/sitepages/support/signup.aspx" class="nav-link"><span class="icon s7-id"></span><span class="name">Signing Up</span></a></li>
<li class="nav-item"><a href="/teams/Training/sitepages/support/faqs.aspx" class="nav-link"><span class="icon s7-search"></span><span class="name">FAQS</span></a></li>
</ul>
</li>

</ul>
</div>
</nav>


JQUERY



 <script>
$(function(){
$("#nav-placeholder").load("../../navbarL1.aspx");
$('.dropdown-toggle').dropdown()
});



</script>






javascript jquery html twitter-bootstrap






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 5 at 16:14

























asked Mar 5 at 14:41









Nick

174




174












  • You can check the url where you are currently, and make it active depending on this(/faqs, /signup,..)..for example
    – Albeis
    Mar 5 at 14:45










  • Include the relevant jQuery code too.
    – Zim
    Mar 5 at 15:36










  • @ZimSystem code updated with JQUERY added
    – Nick
    Mar 5 at 16:15


















  • You can check the url where you are currently, and make it active depending on this(/faqs, /signup,..)..for example
    – Albeis
    Mar 5 at 14:45










  • Include the relevant jQuery code too.
    – Zim
    Mar 5 at 15:36










  • @ZimSystem code updated with JQUERY added
    – Nick
    Mar 5 at 16:15
















You can check the url where you are currently, and make it active depending on this(/faqs, /signup,..)..for example
– Albeis
Mar 5 at 14:45




You can check the url where you are currently, and make it active depending on this(/faqs, /signup,..)..for example
– Albeis
Mar 5 at 14:45












Include the relevant jQuery code too.
– Zim
Mar 5 at 15:36




Include the relevant jQuery code too.
– Zim
Mar 5 at 15:36












@ZimSystem code updated with JQUERY added
– Nick
Mar 5 at 16:15




@ZimSystem code updated with JQUERY added
– Nick
Mar 5 at 16:15












3 Answers
3






active

oldest

votes

















up vote
0
down vote













Try to run this code. I hope it will work.






function getURL() {
// get the whole URL
var currentURL = window.location.href;

// get the file name after = sign
var activeTemplate = currentURL.substr(currentURL.indexOf("=") + 1);

// add active class to url
$('a[href="/teams/Training/sitepages/training/'+ activeTemplate +'"]').addClass('active');
})








share|improve this answer




























    up vote
    0
    down vote













    You can use .toggleClass() with a toggle state value as the second param. At that point it's a matter of figuring out how to compare the href with the current URL. You can use location.pathname, but you may want to get the href using getAttribute so it doesn't include the hostname. Otherwise you could also use this.href === location.href. It is important, however, to include this code in a callback to .load() so you don't encounter a race condition.



    $(function(){
    $("#nav-placeholder").load("../../navbarL1.aspx", function(){
    $('.mai-sub-nav li a').each(function(){
    $(this).toggleClass('active', this.getAttribute('href') === location.pathname);
    })
    });
    $('.dropdown-toggle').dropdown()
    });





    share|improve this answer




























      up vote
      0
      down vote













      You have to follow this kind of technique to achieve that



      Here is my nav bar



      <ul class="nav navbar-nav">
      <li class="active" data-pos="1">
      <a href="{{ route('public.site') }}">Home</a>
      </li>
      <li data-pos="2">
      <a href="{{ route('site.page',['about']) }}">About Egypt</a>
      </li>
      <li data-pos="3">
      <a href="{{ route('site.accomodation') }}">Accomodation</a>
      </li>
      <li data-pos="4">
      <a href="{{ route('site.nile_curises') }}">Nile Cruises</a>
      `enter code here`
      </li>
      <li data-pos="5">
      <a href="{{ route('site.tour_packages') }}">Tour Packages</a>
      </li>
      <li class="dropdown" data-pos="6">
      <a href="javascript:void(0)" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true">FAQ</span>
      </a>
      <ul class="dropdown-menu">
      <li><a href="{{ route('site.page',['thing-to-do']) }}">Thing to do</a></li>
      <li><a href="{{ route('site.page',['travel-tip']) }}">How to travel</a</li>
      <li><a href="{{ route('site.page',['travel-help']) }}">Travel Help</a></li>
      </ul>
      </li>
      </ul>


      Here is my script logic in which i get full url and spilt on the base of '/' seprator
      to get an array and check second last and last segment



      var url = window.location.href; 
      var urlArray = url.split("/");
      var lastUrlString = urlArray[urlArray.length-1];
      var pageName = urlArray[urlArray.length-2];


      After getting last and second last segment i loop through navbar li elements and add or remove class on the basis of page names



              var navLinks = $('ul.nav li');
      navLinks.each(function(index,element){

      var position = $(element).data('pos');

      if(lastUrlString == 'home')
      {
      if($(element).data("pos") == 1)
      {
      if(!$(element).hasClass("active") )
      {
      $(element).addClass('active');
      }
      }
      else
      {
      $(element).removeClass("active");
      }
      }
      else if(lastUrlString == 'about')
      {
      if($(element).data("pos") == 2)
      {
      if(!$(element).hasClass("active") )
      {
      $(element).addClass('active');
      }
      }
      else
      {
      $(element).removeClass("active");
      }
      }
      else if(lastUrlString == 'page' && pageName == 'accomodation')
      {
      if($(element).data("pos") == 3)
      {
      if(!$(element).hasClass("active") )
      {
      $(element).addClass('active');
      }
      }
      else
      {
      $(element).removeClass("active");
      }
      }
      else if(lastUrlString == 'page' && pageName == 'nile_curises')
      {
      if($(element).data("pos") == 4)
      {
      if(!$(element).hasClass("active") )
      {
      $(element).addClass('active');
      }
      }
      else
      {
      $(element).removeClass("active");
      }
      }
      else if(lastUrlString == 'page' && pageName == 'tour_packages')
      {
      if($(element).data("pos") == 5)
      {
      if(!$(element).hasClass("active") )
      {
      $(element).addClass('active');
      }
      }
      else
      {
      $(element).removeClass("active");
      }
      }
      else
      {
      $(element).removeClass("active");
      }
      });





      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49112950%2fchange-active-navbar-link-when-using-jquery-load%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote













        Try to run this code. I hope it will work.






        function getURL() {
        // get the whole URL
        var currentURL = window.location.href;

        // get the file name after = sign
        var activeTemplate = currentURL.substr(currentURL.indexOf("=") + 1);

        // add active class to url
        $('a[href="/teams/Training/sitepages/training/'+ activeTemplate +'"]').addClass('active');
        })








        share|improve this answer

























          up vote
          0
          down vote













          Try to run this code. I hope it will work.






          function getURL() {
          // get the whole URL
          var currentURL = window.location.href;

          // get the file name after = sign
          var activeTemplate = currentURL.substr(currentURL.indexOf("=") + 1);

          // add active class to url
          $('a[href="/teams/Training/sitepages/training/'+ activeTemplate +'"]').addClass('active');
          })








          share|improve this answer























            up vote
            0
            down vote










            up vote
            0
            down vote









            Try to run this code. I hope it will work.






            function getURL() {
            // get the whole URL
            var currentURL = window.location.href;

            // get the file name after = sign
            var activeTemplate = currentURL.substr(currentURL.indexOf("=") + 1);

            // add active class to url
            $('a[href="/teams/Training/sitepages/training/'+ activeTemplate +'"]').addClass('active');
            })








            share|improve this answer












            Try to run this code. I hope it will work.






            function getURL() {
            // get the whole URL
            var currentURL = window.location.href;

            // get the file name after = sign
            var activeTemplate = currentURL.substr(currentURL.indexOf("=") + 1);

            // add active class to url
            $('a[href="/teams/Training/sitepages/training/'+ activeTemplate +'"]').addClass('active');
            })








            function getURL() {
            // get the whole URL
            var currentURL = window.location.href;

            // get the file name after = sign
            var activeTemplate = currentURL.substr(currentURL.indexOf("=") + 1);

            // add active class to url
            $('a[href="/teams/Training/sitepages/training/'+ activeTemplate +'"]').addClass('active');
            })





            function getURL() {
            // get the whole URL
            var currentURL = window.location.href;

            // get the file name after = sign
            var activeTemplate = currentURL.substr(currentURL.indexOf("=") + 1);

            // add active class to url
            $('a[href="/teams/Training/sitepages/training/'+ activeTemplate +'"]').addClass('active');
            })






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 5 at 16:25









            Sodhi saab

            1,49611022




            1,49611022
























                up vote
                0
                down vote













                You can use .toggleClass() with a toggle state value as the second param. At that point it's a matter of figuring out how to compare the href with the current URL. You can use location.pathname, but you may want to get the href using getAttribute so it doesn't include the hostname. Otherwise you could also use this.href === location.href. It is important, however, to include this code in a callback to .load() so you don't encounter a race condition.



                $(function(){
                $("#nav-placeholder").load("../../navbarL1.aspx", function(){
                $('.mai-sub-nav li a').each(function(){
                $(this).toggleClass('active', this.getAttribute('href') === location.pathname);
                })
                });
                $('.dropdown-toggle').dropdown()
                });





                share|improve this answer

























                  up vote
                  0
                  down vote













                  You can use .toggleClass() with a toggle state value as the second param. At that point it's a matter of figuring out how to compare the href with the current URL. You can use location.pathname, but you may want to get the href using getAttribute so it doesn't include the hostname. Otherwise you could also use this.href === location.href. It is important, however, to include this code in a callback to .load() so you don't encounter a race condition.



                  $(function(){
                  $("#nav-placeholder").load("../../navbarL1.aspx", function(){
                  $('.mai-sub-nav li a').each(function(){
                  $(this).toggleClass('active', this.getAttribute('href') === location.pathname);
                  })
                  });
                  $('.dropdown-toggle').dropdown()
                  });





                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    You can use .toggleClass() with a toggle state value as the second param. At that point it's a matter of figuring out how to compare the href with the current URL. You can use location.pathname, but you may want to get the href using getAttribute so it doesn't include the hostname. Otherwise you could also use this.href === location.href. It is important, however, to include this code in a callback to .load() so you don't encounter a race condition.



                    $(function(){
                    $("#nav-placeholder").load("../../navbarL1.aspx", function(){
                    $('.mai-sub-nav li a').each(function(){
                    $(this).toggleClass('active', this.getAttribute('href') === location.pathname);
                    })
                    });
                    $('.dropdown-toggle').dropdown()
                    });





                    share|improve this answer












                    You can use .toggleClass() with a toggle state value as the second param. At that point it's a matter of figuring out how to compare the href with the current URL. You can use location.pathname, but you may want to get the href using getAttribute so it doesn't include the hostname. Otherwise you could also use this.href === location.href. It is important, however, to include this code in a callback to .load() so you don't encounter a race condition.



                    $(function(){
                    $("#nav-placeholder").load("../../navbarL1.aspx", function(){
                    $('.mai-sub-nav li a').each(function(){
                    $(this).toggleClass('active', this.getAttribute('href') === location.pathname);
                    })
                    });
                    $('.dropdown-toggle').dropdown()
                    });






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 5 at 16:30









                    Joseph Marikle

                    56.7k1286116




                    56.7k1286116






















                        up vote
                        0
                        down vote













                        You have to follow this kind of technique to achieve that



                        Here is my nav bar



                        <ul class="nav navbar-nav">
                        <li class="active" data-pos="1">
                        <a href="{{ route('public.site') }}">Home</a>
                        </li>
                        <li data-pos="2">
                        <a href="{{ route('site.page',['about']) }}">About Egypt</a>
                        </li>
                        <li data-pos="3">
                        <a href="{{ route('site.accomodation') }}">Accomodation</a>
                        </li>
                        <li data-pos="4">
                        <a href="{{ route('site.nile_curises') }}">Nile Cruises</a>
                        `enter code here`
                        </li>
                        <li data-pos="5">
                        <a href="{{ route('site.tour_packages') }}">Tour Packages</a>
                        </li>
                        <li class="dropdown" data-pos="6">
                        <a href="javascript:void(0)" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true">FAQ</span>
                        </a>
                        <ul class="dropdown-menu">
                        <li><a href="{{ route('site.page',['thing-to-do']) }}">Thing to do</a></li>
                        <li><a href="{{ route('site.page',['travel-tip']) }}">How to travel</a</li>
                        <li><a href="{{ route('site.page',['travel-help']) }}">Travel Help</a></li>
                        </ul>
                        </li>
                        </ul>


                        Here is my script logic in which i get full url and spilt on the base of '/' seprator
                        to get an array and check second last and last segment



                        var url = window.location.href; 
                        var urlArray = url.split("/");
                        var lastUrlString = urlArray[urlArray.length-1];
                        var pageName = urlArray[urlArray.length-2];


                        After getting last and second last segment i loop through navbar li elements and add or remove class on the basis of page names



                                var navLinks = $('ul.nav li');
                        navLinks.each(function(index,element){

                        var position = $(element).data('pos');

                        if(lastUrlString == 'home')
                        {
                        if($(element).data("pos") == 1)
                        {
                        if(!$(element).hasClass("active") )
                        {
                        $(element).addClass('active');
                        }
                        }
                        else
                        {
                        $(element).removeClass("active");
                        }
                        }
                        else if(lastUrlString == 'about')
                        {
                        if($(element).data("pos") == 2)
                        {
                        if(!$(element).hasClass("active") )
                        {
                        $(element).addClass('active');
                        }
                        }
                        else
                        {
                        $(element).removeClass("active");
                        }
                        }
                        else if(lastUrlString == 'page' && pageName == 'accomodation')
                        {
                        if($(element).data("pos") == 3)
                        {
                        if(!$(element).hasClass("active") )
                        {
                        $(element).addClass('active');
                        }
                        }
                        else
                        {
                        $(element).removeClass("active");
                        }
                        }
                        else if(lastUrlString == 'page' && pageName == 'nile_curises')
                        {
                        if($(element).data("pos") == 4)
                        {
                        if(!$(element).hasClass("active") )
                        {
                        $(element).addClass('active');
                        }
                        }
                        else
                        {
                        $(element).removeClass("active");
                        }
                        }
                        else if(lastUrlString == 'page' && pageName == 'tour_packages')
                        {
                        if($(element).data("pos") == 5)
                        {
                        if(!$(element).hasClass("active") )
                        {
                        $(element).addClass('active');
                        }
                        }
                        else
                        {
                        $(element).removeClass("active");
                        }
                        }
                        else
                        {
                        $(element).removeClass("active");
                        }
                        });





                        share|improve this answer

























                          up vote
                          0
                          down vote













                          You have to follow this kind of technique to achieve that



                          Here is my nav bar



                          <ul class="nav navbar-nav">
                          <li class="active" data-pos="1">
                          <a href="{{ route('public.site') }}">Home</a>
                          </li>
                          <li data-pos="2">
                          <a href="{{ route('site.page',['about']) }}">About Egypt</a>
                          </li>
                          <li data-pos="3">
                          <a href="{{ route('site.accomodation') }}">Accomodation</a>
                          </li>
                          <li data-pos="4">
                          <a href="{{ route('site.nile_curises') }}">Nile Cruises</a>
                          `enter code here`
                          </li>
                          <li data-pos="5">
                          <a href="{{ route('site.tour_packages') }}">Tour Packages</a>
                          </li>
                          <li class="dropdown" data-pos="6">
                          <a href="javascript:void(0)" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true">FAQ</span>
                          </a>
                          <ul class="dropdown-menu">
                          <li><a href="{{ route('site.page',['thing-to-do']) }}">Thing to do</a></li>
                          <li><a href="{{ route('site.page',['travel-tip']) }}">How to travel</a</li>
                          <li><a href="{{ route('site.page',['travel-help']) }}">Travel Help</a></li>
                          </ul>
                          </li>
                          </ul>


                          Here is my script logic in which i get full url and spilt on the base of '/' seprator
                          to get an array and check second last and last segment



                          var url = window.location.href; 
                          var urlArray = url.split("/");
                          var lastUrlString = urlArray[urlArray.length-1];
                          var pageName = urlArray[urlArray.length-2];


                          After getting last and second last segment i loop through navbar li elements and add or remove class on the basis of page names



                                  var navLinks = $('ul.nav li');
                          navLinks.each(function(index,element){

                          var position = $(element).data('pos');

                          if(lastUrlString == 'home')
                          {
                          if($(element).data("pos") == 1)
                          {
                          if(!$(element).hasClass("active") )
                          {
                          $(element).addClass('active');
                          }
                          }
                          else
                          {
                          $(element).removeClass("active");
                          }
                          }
                          else if(lastUrlString == 'about')
                          {
                          if($(element).data("pos") == 2)
                          {
                          if(!$(element).hasClass("active") )
                          {
                          $(element).addClass('active');
                          }
                          }
                          else
                          {
                          $(element).removeClass("active");
                          }
                          }
                          else if(lastUrlString == 'page' && pageName == 'accomodation')
                          {
                          if($(element).data("pos") == 3)
                          {
                          if(!$(element).hasClass("active") )
                          {
                          $(element).addClass('active');
                          }
                          }
                          else
                          {
                          $(element).removeClass("active");
                          }
                          }
                          else if(lastUrlString == 'page' && pageName == 'nile_curises')
                          {
                          if($(element).data("pos") == 4)
                          {
                          if(!$(element).hasClass("active") )
                          {
                          $(element).addClass('active');
                          }
                          }
                          else
                          {
                          $(element).removeClass("active");
                          }
                          }
                          else if(lastUrlString == 'page' && pageName == 'tour_packages')
                          {
                          if($(element).data("pos") == 5)
                          {
                          if(!$(element).hasClass("active") )
                          {
                          $(element).addClass('active');
                          }
                          }
                          else
                          {
                          $(element).removeClass("active");
                          }
                          }
                          else
                          {
                          $(element).removeClass("active");
                          }
                          });





                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You have to follow this kind of technique to achieve that



                            Here is my nav bar



                            <ul class="nav navbar-nav">
                            <li class="active" data-pos="1">
                            <a href="{{ route('public.site') }}">Home</a>
                            </li>
                            <li data-pos="2">
                            <a href="{{ route('site.page',['about']) }}">About Egypt</a>
                            </li>
                            <li data-pos="3">
                            <a href="{{ route('site.accomodation') }}">Accomodation</a>
                            </li>
                            <li data-pos="4">
                            <a href="{{ route('site.nile_curises') }}">Nile Cruises</a>
                            `enter code here`
                            </li>
                            <li data-pos="5">
                            <a href="{{ route('site.tour_packages') }}">Tour Packages</a>
                            </li>
                            <li class="dropdown" data-pos="6">
                            <a href="javascript:void(0)" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true">FAQ</span>
                            </a>
                            <ul class="dropdown-menu">
                            <li><a href="{{ route('site.page',['thing-to-do']) }}">Thing to do</a></li>
                            <li><a href="{{ route('site.page',['travel-tip']) }}">How to travel</a</li>
                            <li><a href="{{ route('site.page',['travel-help']) }}">Travel Help</a></li>
                            </ul>
                            </li>
                            </ul>


                            Here is my script logic in which i get full url and spilt on the base of '/' seprator
                            to get an array and check second last and last segment



                            var url = window.location.href; 
                            var urlArray = url.split("/");
                            var lastUrlString = urlArray[urlArray.length-1];
                            var pageName = urlArray[urlArray.length-2];


                            After getting last and second last segment i loop through navbar li elements and add or remove class on the basis of page names



                                    var navLinks = $('ul.nav li');
                            navLinks.each(function(index,element){

                            var position = $(element).data('pos');

                            if(lastUrlString == 'home')
                            {
                            if($(element).data("pos") == 1)
                            {
                            if(!$(element).hasClass("active") )
                            {
                            $(element).addClass('active');
                            }
                            }
                            else
                            {
                            $(element).removeClass("active");
                            }
                            }
                            else if(lastUrlString == 'about')
                            {
                            if($(element).data("pos") == 2)
                            {
                            if(!$(element).hasClass("active") )
                            {
                            $(element).addClass('active');
                            }
                            }
                            else
                            {
                            $(element).removeClass("active");
                            }
                            }
                            else if(lastUrlString == 'page' && pageName == 'accomodation')
                            {
                            if($(element).data("pos") == 3)
                            {
                            if(!$(element).hasClass("active") )
                            {
                            $(element).addClass('active');
                            }
                            }
                            else
                            {
                            $(element).removeClass("active");
                            }
                            }
                            else if(lastUrlString == 'page' && pageName == 'nile_curises')
                            {
                            if($(element).data("pos") == 4)
                            {
                            if(!$(element).hasClass("active") )
                            {
                            $(element).addClass('active');
                            }
                            }
                            else
                            {
                            $(element).removeClass("active");
                            }
                            }
                            else if(lastUrlString == 'page' && pageName == 'tour_packages')
                            {
                            if($(element).data("pos") == 5)
                            {
                            if(!$(element).hasClass("active") )
                            {
                            $(element).addClass('active');
                            }
                            }
                            else
                            {
                            $(element).removeClass("active");
                            }
                            }
                            else
                            {
                            $(element).removeClass("active");
                            }
                            });





                            share|improve this answer












                            You have to follow this kind of technique to achieve that



                            Here is my nav bar



                            <ul class="nav navbar-nav">
                            <li class="active" data-pos="1">
                            <a href="{{ route('public.site') }}">Home</a>
                            </li>
                            <li data-pos="2">
                            <a href="{{ route('site.page',['about']) }}">About Egypt</a>
                            </li>
                            <li data-pos="3">
                            <a href="{{ route('site.accomodation') }}">Accomodation</a>
                            </li>
                            <li data-pos="4">
                            <a href="{{ route('site.nile_curises') }}">Nile Cruises</a>
                            `enter code here`
                            </li>
                            <li data-pos="5">
                            <a href="{{ route('site.tour_packages') }}">Tour Packages</a>
                            </li>
                            <li class="dropdown" data-pos="6">
                            <a href="javascript:void(0)" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true">FAQ</span>
                            </a>
                            <ul class="dropdown-menu">
                            <li><a href="{{ route('site.page',['thing-to-do']) }}">Thing to do</a></li>
                            <li><a href="{{ route('site.page',['travel-tip']) }}">How to travel</a</li>
                            <li><a href="{{ route('site.page',['travel-help']) }}">Travel Help</a></li>
                            </ul>
                            </li>
                            </ul>


                            Here is my script logic in which i get full url and spilt on the base of '/' seprator
                            to get an array and check second last and last segment



                            var url = window.location.href; 
                            var urlArray = url.split("/");
                            var lastUrlString = urlArray[urlArray.length-1];
                            var pageName = urlArray[urlArray.length-2];


                            After getting last and second last segment i loop through navbar li elements and add or remove class on the basis of page names



                                    var navLinks = $('ul.nav li');
                            navLinks.each(function(index,element){

                            var position = $(element).data('pos');

                            if(lastUrlString == 'home')
                            {
                            if($(element).data("pos") == 1)
                            {
                            if(!$(element).hasClass("active") )
                            {
                            $(element).addClass('active');
                            }
                            }
                            else
                            {
                            $(element).removeClass("active");
                            }
                            }
                            else if(lastUrlString == 'about')
                            {
                            if($(element).data("pos") == 2)
                            {
                            if(!$(element).hasClass("active") )
                            {
                            $(element).addClass('active');
                            }
                            }
                            else
                            {
                            $(element).removeClass("active");
                            }
                            }
                            else if(lastUrlString == 'page' && pageName == 'accomodation')
                            {
                            if($(element).data("pos") == 3)
                            {
                            if(!$(element).hasClass("active") )
                            {
                            $(element).addClass('active');
                            }
                            }
                            else
                            {
                            $(element).removeClass("active");
                            }
                            }
                            else if(lastUrlString == 'page' && pageName == 'nile_curises')
                            {
                            if($(element).data("pos") == 4)
                            {
                            if(!$(element).hasClass("active") )
                            {
                            $(element).addClass('active');
                            }
                            }
                            else
                            {
                            $(element).removeClass("active");
                            }
                            }
                            else if(lastUrlString == 'page' && pageName == 'tour_packages')
                            {
                            if($(element).data("pos") == 5)
                            {
                            if(!$(element).hasClass("active") )
                            {
                            $(element).addClass('active');
                            }
                            }
                            else
                            {
                            $(element).removeClass("active");
                            }
                            }
                            else
                            {
                            $(element).removeClass("active");
                            }
                            });






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 at 7:51









                            Syed Nazir Hussain

                            10817




                            10817






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49112950%2fchange-active-navbar-link-when-using-jquery-load%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Bressuire

                                Vorschmack

                                Quarantine