Actualizar la barra de direcciones con una nueva URL sin hash ni recargar la página


O soñé con chrome (dev channel) implementar una forma de actualizar la barra de direcciones a través de javascript (la ruta, no el dominio) sin recargar la página o realmente han hecho esto.

Sin embargo, no puedo encontrar el artículo que creo que leí.

Estoy loco o hay una manera de hacer esto (en Chrome)?

P.d. No estoy hablando de ventana. ubicación.hash, et al. Si existe la respuesta a esta pregunta será falsa.

Author: Community, 2010-07-27

3 answers

Ahora puede hacer esto en la mayoría de los navegadores "modernos"!

Aquí está el artículo original que leí (publicado en julio 10, 2010): HTML5: Cambiar la URL del navegador sin actualizar la página.

Para una mirada más profunda a pushState/replaceState/popstate (también conocida como la API de historial HTML5) vea los documentos de MDN.

TL;DR, puedes hacer esto:

window.history.pushState("object or string", "Title", "/new-url");

Vea mi respuesta a Modificar la URL sin recargar la página para obtener una guía básica.

 750
Author: David Murdoch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:54:57

Cambiando solo lo que hay después de hash-navegadores antiguos

document.location.hash = 'lookAtMeNow';

Cambiando la URL completa. Chrome, Firefox, IE10 +

history.pushState('data to be passed', 'Title of the page', '/test');

Lo anterior agregará una nueva entrada al historial para que pueda presionar el botón Atrás para ir al estado anterior. Para cambiar la URL en su lugar sin agregar una nueva entrada al historial, use

history.replaceState('data to be passed', 'Title of the page', '/test');

¡Intenta ejecutarlos en la consola ahora!

 112
Author: Pawel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-17 20:02:00

La actualización a la respuesta de Davids para incluso detectar navegadores no es compatible con pushstate:

if (history.pushState) {
  window.history.pushState("object or string", "Title", "/new-url");
} else {
  document.location.href = "/new-url";
}
 10
Author: metamagicson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-04 11:01:52