Style link based on page url with jquery, doesn't work with "www." -
i'm using bit of jquery style links on site based on page url:
$(document).ready(function() { var url =window.location; $("#left a").each(function() { if($(this).attr('href') == url){ $(this).addclass('current'); } }); });
and html each link this:
<a href="http://mysite.com/our-company.html">our company</a>
the problem if tries go www.mysite.com/our-company.html, styling won't work.
how can remove "www." variable? tried putting ".replace" in several different spots i'm newbie.
i tried:
$(document).ready(function() { var urla = window.location; var urlb = urla.replace("www.",""); $("#left a").each(function() { if($(this).attr('href') == urlb){ $(this).addclass('current'); } }); });
which tries redirect "http://mysite.com/www."
you need href of url, not url itself. using window.location.href
:
$(document).ready(function() { // `.href` added var urla = window.location.href; var urlb = urla.replace("www.",""); $("#left a").each(function() { if($(this).attr('href') == urlb){ $(this).addclass('current'); } }); });
Comments
Post a Comment