Monday, October 28, 2019

jquery - How can I add/remove a class to an element when URL contains a specific string?




I have some tabs:








home

content





And I would like to add a class to the tab, when my URL contains the string ?dir.



So for example: if my URL is www.mypage.com/?dir=something then #contenttab should be active. And if the URL is for example only
www.mypage.com
then #contenttab should not be active but #hometabshould be active.



    



My script is not working. #contenttabis always active and #hometab is never active.


Answer



Try checking with this :



if ( url.indexOf( '?dir' ) !== -1 ) { ... }


Or try with :




var url = location.search; // or location.href;

if ( url.indexOf( '?dir' ) !== -1 ) { ... }

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...