javascript - How do I check if the current url or path is equal to a certain link and if not, append a forward slash to the href in the nav bar? -
i javascript , jquery newby , have issues page scroll being directed different page. work being done in wordpress php. using page-scroll href=#id , changed href=/#id slash allows directing properly. issue if on current page of home. forward slash jumps rather page-scrolls. thinking leave href=#id without slash , have check if current url != home, append forward slash href. genuinely appreciated!
this allows page scroll current page , doesn't direct pricing & services page
<li><a class="page-scroll" href="#howitworks">how works</a></li>
adding "/" allows directing pricing& services page jumps if on homepage. want jump if not on homepage. smooth scroll on current page.
<li><a class="page-scroll" href="/#aboutus">about us</a></li>
page directing from.
<li><a class="page-scroll" href="pricing-services">pricing & services</a></li>
this javascript code using requires jquery easing plugin
$(function() { $('a.page-scroll').bind('click', function(event) { var $anchor = $(this); $('html, body').stop().animate({ scrolltop: $($anchor.attr('href')).offset().top-50 }, 1500, 'easeinoutexpo'); event.preventdefault(); }); });
to current url can use window.location.pathname
:
var path = window.location.pathname; var add = '';
check if "home"
in url. indexof
returns -1 if doesnt find anything. else return position of first occurance
if(path.indexof("home") == -1){ add = '/'; }
then add slash href this:
var href = $('.page-scroll').attr('href'); $('.page-scroll').attr('href',add+href);