How to select a menu item with the given get parameter?

Good day!

There is a script that selects the menu item:
$(document).ready(function($) {
 var url=document.location.href;
 $.each($("nav a"),function(){
if(this.href==url){$(this).addClass('active');};
});
});


The question is: if the link is added get parameter, then the selection disappears naturally, since href is not the same. How can I change the script to ignore the get option and still highlighted active menu item?

Thanks for the help!
July 4th 19 at 23:38
2 answers
July 4th 19 at 23:40
Solution
document.location.pathname
Thanks for the reply. I'm sorry, but not working. - Jairo commented on July 4th 19 at 23:43
: It 'site.ru/page?a=1' will return '/page', if you will remove from the href part with the domain will work - Reagan_Bins68 commented on July 4th 19 at 23:46
: Thank you, it works. But I would like to keep absolute links. Used the solution above. Thanks for the help. - Jairo commented on July 4th 19 at 23:49
an absolute reference with a slash at the beginning of the domain it is not required, it is added automatically, unless, of course, in the same domain and everything happens. - Reagan_Bins68 commented on July 4th 19 at 23:52
July 4th 19 at 23:42
Solution
So
$(document).ready(function($) {
 var url=window.location.protocol+'//'+window.location.hostname+window.location.pathname;
 $.each($("nav a"),function(){
if(this.href==url){$(this).addClass('active');};
});
});
Thanks for the reply. I'm sorry, but not working. - Jairo commented on July 4th 19 at 23:45
: Is the url which works as it should and the url where manifest problems? Better yet the address of the site. - Reagan_Bins68 commented on July 4th 19 at 23:48
In the script:
$(document).ready(function($) {
var url=document.location.href;
$.each($("nav a"),function(){
if(this.href==url){$(this).addClass('active');};
});
});
Url nav a: site.ru/about/
Highlighted link: "About us"

Url nav a: site.ru/about/??url=http...
The selection of a link disappears, and the job must remain on the link "About us"

The script You suggested:
$(document).ready(function($) {
var url=window.location.pathname;
$.each($("nav a"),function(){
if(this.href==url){$(this).addClass('active');};
});
});

There is no selection at all. - Jairo commented on July 4th 19 at 23:51
: I corrected the code in the answer, should work now. - Reagan_Bins68 commented on July 4th 19 at 23:54
Oh yeah! Thank you so much! - Jairo commented on July 4th 19 at 23:57

Find more questions by tags JavaScriptjQuery