Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery if link = page url

ok pretty simple but i dont know how...

i just want to make an active state (probably just make it bold)

my menu is ul-li

i cant figure out how to write it so if the url matches with one of the links, make the link bold

please help

thanks for your time

like image 786
Alex Avatar asked Dec 14 '10 19:12

Alex


People also ask

How to get URL of page in jQuery?

The current URL in jQuery can be obtained by using the 'href' property of the Location object which contains information about the current URL. The 'href' property returns a string with the full URL of the current page.

How to get id from URL jQuery?

To Get the ID from URL with JavaScript, we can call the JavaScript string's split method on the URL string. Then we get the last part of it with the JavaScript array at method. We call split with '/' to split the url string by the / . Then we call at with -1 to get the element from the strs array.

How to open link using jQuery?

jQuery attribute equals selectors If we want to force a link to a given URL to open in a new tab, we would use the following: $(document). ready(function(){ $('a[href=http://www.google.com]').click(function(){ window. open(this.

How can you tell if a link is internal or external?

By comparing the hostname of the string URL to the hostname of the web page URL, you can determine whether the link is external or not.


1 Answers

Here's a short way to select links like that:

$('ul > li a[href$="' + window.location.pathname + '"]').css('font-weight','bold');

Or perhaps better like this, which does an exact match of both pathname attributes:

$('ul > li a[href]').filter(function() {
    return this.href.pathname === window.location.pathname;
}).css('font-weight','bold');

If you're using the full domain in the href, you could change it to:

return this.href === window.location;
like image 167
user113716 Avatar answered Nov 10 '22 12:11

user113716