Quiero deshabilitar el botón Atrás del navegador. He probado algunos códigos javascript para implementar lo mismo. Pero esos códigos no cumplían con mis requisitos. Funciona en Firefox pero no funciona en Chrome y Edge. A veces funciona los tres pero aleatoriamente funciona y no funciona.
¿Alguien puede compartirme el script para deshabilitar el botón Atrás en todos los navegadores sin fallas como el comportamiento aleatorio?
Aquí mostré algunos scripts que he probado.
history.pushState(null, null, window.location.href); history.back(); window.onpopstate = () => history.forward(); history.pushState(null, null, location.href); history.back(); history.forward(); window.onpopstate = function () { history.go(1); };Puede consultar el siguiente código. Pruebo el ejemplo de código en Edge, Chrome y Firefox, funciona bien.
Guarde este archivo como .html para la primera página:
<!DOCTYPE html> <html> <head> <title>First Page</title> <script type="text/javascript"> function preventBack() { window.history.forward(); } setTimeout("preventBack()", 0); window.onunload = function () { null }; </script> </head> <body> <h3>This is first page</h3> <a href="b.html">Goto second Page</a> </body> </html>Guarde este archivo como b.html para la segunda página:
<!DOCTYPE html> <html> <head> <title> Blocking Back Button using javascript </title> </head> <body> <h3>This is second page</h3> <p> On this page, back button functionality is disabled. </p> </body> </html>Ejecute a.html y haga clic para navegar a b.html. Luego haga clic en el botón Atrás del navegador en b.html, no navegará a a.html.