Quelqu'un pourrait-il partager l'expérience / le code sur la façon dont nous pouvons détecter le bouton de retour du navigateur (pour tout type de navigateur)?
Nous devons répondre à tous les navigateurs qui ne prennent pas en charge HTML5
L'événement 'popstate' ne fonctionne que lorsque vous appuyez quelque chose avant. Donc vous devez faire quelque chose comme ceci:
jQuery(document).ready(function($) { if (window.history && window.history.pushState) { window.history.pushState('forward', null, './#forward'); $(window).on('popstate', function() { alert('Back button was pressed.'); }); } });
Pour la compatibilité descendante du navigateur, je recommande: history.js
Il existe de nombreuses façons de savoir si l'utilisateur a cliqué sur le bouton Précédent. Mais tout dépend de vos besoins. Essayez d'explorer les liens ci-dessous, ils devraient vous aider.
Détecter si l'utilisateur appuie sur le bouton "Retour" sur la page actuelle:
Détectez si la page actuelle est visitée après avoir appuyé sur le bouton "Retour" sur la page précédente ("Forward"):
Trouvé cela pour fonctionner correctement, naviguez vers le navigateur et le mobile back_button_override.js .
(Ajout d'une minuterie pour safari 5.0)
// managage back button click (and backspace) var count = 0; // needed for safari window.onload = function () { if (typeof history.pushState === "function") { history.pushState("back", null, null); window.onpopstate = function () { history.pushState('back', null, null); if(count == 1){window.location = 'your url';} }; } } setTimeout(function(){count = 1;},200);
Désactiver le bouton url en suivant la fonction
window.onload = function () { if (typeof history.pushState === "function") { history.pushState("jibberish", null, null); window.onpopstate = function () { history.pushState('newjibberish', null, null); // Handle the back (or forward) buttons here // Will NOT handle refresh, use onbeforeunload for this. }; } else { var ignoreHashChange = true; window.onhashchange = function () { if (!ignoreHashChange) { ignoreHashChange = true; window.location.hash = Math.random(); // Detect and redirect change here // Works in older FF and IE9 // * it does mess with your hash symbol (anchor?) pound sign // delimiter on the end of the URL } else { ignoreHashChange = false; } }; } };
Il est disponible dans l'API HTML5 History. L'événement s'appelle 'popstate'