I have the following problem: some time ago I adapted a jQuery script for a menu that changes the inside of a page hiding and showing the content according to the link that is pressed, the content is on a single page and I do not know how to do it so that when reloading the web it stays in the content indicated by the link
here the code:
$(document).ready(function(){
var ultimo=1;
$('#menu-sup li a').click(function(){
// obtenemos el nuevo id
nuevo=$(this).parent().attr("id");
if(nuevo!=ultimo){
// escondemos el ultimo id
$("#cont"+ultimo).fadeOut(function(){
$("#"+ultimo).removeClass("select");
$("#"+nuevo).addClass("select");
// mostramos el nuevo id
$("#cont"+nuevo).fadeIn();
ultimo=nuevo;
});
}
});
});
.cont{display:none}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div class="seccion-p">
<div class="menu">
<ul id="menu-sup">
<li id="1" class="select "><a href="#inicio">Inicio</a> </li>
<li id="2" ><a href="#Admisiones">Admisiones</a></li>
<li id="3" ><a href="#Usuarios">Usuarios</a></li>
</ul>
</div>
<div class="contenido-menu">
<div id="cont1">contenido 1</div>
<div id="cont2" class="cont">contenido 2</div>
<div id="cont3" class="cont">contenido 3</div>
</div>
</div>
Edited. I found a not very elegant way of doing it, the script of the click I leave it as a function ckick_menu () then getting the hash and assigning it to a variable that contains the link of the menu 'm_click' then I repeat the same code of the function click to then do a trigger of the click which allows the content to be loaded when reloading the site and finally I call the function click_menu () with the parameter 'm_click' redefined. This works not 100% since if you load the content but when you click on the other menu it does not hide the information that was reloaded. Thanks for the answers:
$(document).ready(function(){
function click_menu(menu_click){
var ultimo=1;
$(menu_click).click(function(){
// obtenemos el nuevo id
nuevo=$(this).parent().attr("id");
if(nuevo!=ultimo){
//ultimo = ultimo+1;// escondemos el ultimo id
$("#cont"+ultimo).fadeOut(function(){
$("#"+ultimo).removeClass("select");
$("#"+nuevo).addClass("select");
// mostramos el nuevo id
$("#cont"+nuevo).fadeIn();
ultimo=nuevo;
});
}
});
}
hash = '[href="'+document.location.hash+'"]';
if(hash){
menu_click = '#menu-sup li a'+hash+'';
$(menu_click).click(function(){
var ultimo=1;
// obtenemos el nuevo id
nuevo=$(this).parent().attr("id");
if(nuevo!=ultimo){
// escondemos el ultimo id
$("#cont"+ultimo).fadeOut(function(){
$("#"+ultimo).removeClass("select");
$("#"+nuevo).addClass("select");
// mostramos el nuevo id
$("#cont"+nuevo).fadeIn();
ultimo=nuevo;
});
}
});
$(menu_click).trigger('click',);
menu_click = '#menu-sup li a';
click_menu(menu_click);
}
});