Thursday, 22 September 2016

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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...