Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Page Url with jQuery

I've browsed a few other questions on here with getting current page url with jQuery and/or plain javascript but I have been having no luck.

My goal is this: depending on what page the user is on, I want a border under that menu item. This is how I am attempting to do it:

var urlHome = 'http://example.com/'
var urlShop = 'http://example.com/shop/'
var urlTeam = 'http://example.com/team/'

if (document.url.is(urlHome)) {
    $('#home-link').css('border-bottom', '1px solid #000');
}
else if (document.url.is(urlShop)) {
    $('#shop-link').css('border-bottom', '1px solid #000');
}
else if (document.url.is(urlTeam)) {
    $('#team-link').css('border-bottom', '1px solid #000');
};

Unfortunately this code isn't working for me, I'm open to any and all suggestions, thank you in advance for your help.

like image 488
Mr.Smithyyy Avatar asked Dec 30 '25 15:12

Mr.Smithyyy


1 Answers

var loc = window.location.href,id="";
     if (loc === urlHome) id = "home";
else if (loc === urlShop) id = "shop";
else if (loc === urlTeam) id = "team";
if (id) $('#'+id+'-link').css('border-bottom', '1px solid #000');

This works for me, with my own variables of course :)

like image 125
Tacticus Avatar answered Jan 01 '26 04:01

Tacticus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!