How to write the condition for references?

There is the following code the handler for the accordion menu, how can you prescribe a condition that would when clicking on a link worked and the link and the script, while the script remained the same as now. That is, when clicked, reveals a submenu and when you click on the next element, the previous is hidden.
$('.accordion > li > a').click(function(e) {
e.preventDefault();

 var $this = $(this);

 if ($this.next().hasClass('show')) {
$this.next().removeClass('show');
$this.next().slideUp(350);
 } else {
$this.parent().parent().find('.sub-menu').removeClass('show');
$this.parent().parent().find('.sub-menu').slideUp(350);
$this.next().toggleClass('show');
$this.next().slideToggle(350);
}

 if ($this.hasClass('active')) {
$this.removeClass('active');
 } else { 
$this.parent().parent().find('a').removeClass('active');
$this.toggleClass('active');
};
});


P. S. If you just remove preventDefault, the page reloads with closed submenu items
June 5th 19 at 21:17
2 answers
June 5th 19 at 21:19
Solution
  1. To move the handler to the event after the page is loaded
  2. The need for disclosure of the submenu to Express in the url (mypage.html#menu1Opened)
  3. Analyze window.location and edit the current URL
I just do not understand one, is obtained for each menu item I need to create a unique hash with status and process at boot?
So for each menu item with hash I need separate terms when the page is loaded to write? - meagan.Bruen54 commented on June 5th 19 at 21:22
In this design, as in the initial design to write OnClick - Roderick_Quitzon56 commented on June 5th 19 at 21:25
June 5th 19 at 21:21
Solution
how can you prescribe a condition that would when clicking on a link worked and the link and script

No way. If it works link - the page will be reloaded.
You can save in cookies link to the last object, click menu, and then expand it on page load.
Can you show how to work with cookies? In this particular example, since only learn js - meagan.Bruen54 commented on June 5th 19 at 21:24
https://js-master.ru/?Javascripts___Технологии___Р... - Roderick_Quitzon56 commented on June 5th 19 at 21:27
on the last item click menu, and then expand it on page load.


In this case, if the user opens several pages from different menu items there can get complicated detailed submenus. But after a page reload all installed at the last open submenu. - meagan.Bruen54 commented on June 5th 19 at 21:30
, cookies can be saved for a specific page. - rhianna_Fee commented on June 5th 19 at 21:33
Then it turns out that a particular page will not know from whence it came - that is, if two sessions open it from the menu and just like that, menu will be deployed in both places - Roderick_Quitzon56 commented on June 5th 19 at 21:36

Find more questions by tags JavaScriptjQuery