Monday, 31 July 2017

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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...