Ajax URL # ne met pas à jour

J’ai un petit problème avec mon script ici. Pour une raison quelconque, cela n’active pas les balises # et je ne sais pas pourquoi. J’ai créé ce javascript à l’aide de ce tutoriel . (Le chargement des pages fonctionne bien sans aucun problème.)

Quelqu’un pourrait-il s’il vous plaît l’examiner et me dire pourquoi cela ne fonctionne pas?

var default_content=""; $(document).ready(function(){ //executed after the page has loaded checkURL(); //check if the URL has a reference to a page and load it $('ul li a').click(function (e){ //traverse through all our navigation links.. checkURL(this.hash); //.. and assign them a new onclick event, using their own hash as a parameter (#page1 for example) }); setInterval("checkURL()",250); //check for a change in the URL every 250 ms to detect if the history buttons have been used }); var lasturl=""; //here we store the current URL hash function checkURL(hash) { if(!hash) hash=window.location.hash; //if no parameter is provided, use the hash value from the current address if(hash != lasturl) // if the hash value has changed { lasturl=hash; //update the current hash loadPage(hash); // and load the new page } } function loadPage(url) //the function that loads pages via AJAX { // Instead of ssortingpping off #page, only // ssortingp off the # to use the rest of the URL url=url.replace('#',''); $('#loading').css('visibility','visible'); //show the rotating gif animation $.ajax({ type: "POST", url: "load_page.php", data: 'page='+url, dataType: "html", success: function(msg){ if(parseInt(msg)!=0) //if no errors { $('#content').html(msg); //load the returned html into pageContet } $('#loading').css('visibility','hidden');//and hide the rotating gif } }); } 

Vous pouvez énormément simplifier cela en ajoutant une fonction permettant d’écouter l’événement hashchange , comme ceci:

 $(window).on("hashchange", function() { loadPage(window.location.hash); }); 

De cette façon, vous n’avez pas besoin de gérer les timers ni les événements de clic sur les ancres.

De plus, vous n’avez pas besoin de suivre lasthash car le changement de hachage ne sera déclenché que lorsque le hachage sera modifié.