Ver Mensaje Individual
  #10 (permalink)  
Antiguo 05/12/2011, 13:15
carlosvelez5
 
Fecha de Ingreso: diciembre-2011
Mensajes: 2
Antigüedad: 12 años, 5 meses
Puntos: 0
De acuerdo Respuesta: Cambiar URL con JavaScript

Holaaa

mira pues te tengo la solución. con la función de history.replaceState y history.pushState de javascript. funciona en todos los navegadores menos en i.e, pero como alternativa para los navegadores que no soportan esta función podes implementarla con el hash.

aquí te dejo un ejemplo muy facil y práctico.

Código Javascript:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.   <title>PushState</title>
  5.     <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script><!-- Actualizar -->
  6.     <script>
  7.     $(document).ready(function() {
  8.         // Para navegadores que soportan la función.
  9.         if (typeof window.history.pushState == 'function') {
  10.             pushstate();           
  11.         }else{
  12.             check(); hash();
  13.         }
  14.     });
  15.     // Chequear si existe el hash.
  16.     function check(){
  17.         var direccion = ""+window.location+"";
  18.         var nombre = direccion.split("#!");
  19.         if(nombre.length > 1){
  20.             var url = nombre[1];
  21.             alert(url);
  22.         }
  23.     }
  24.    
  25.     function pushstate(){
  26.         var links = $("a");
  27.         // Evento al hacer click.
  28.         links.live('click', function(event) {
  29.             var url = $(this).attr('href');
  30.             // Cambio el historial del navegador.
  31.             history.pushState({ path: url }, url, url);
  32.             // Muestro la nueva url
  33.             alert(url);
  34.             return false;
  35.         });
  36.        
  37.         // Función para determinar cuando cambia la url de la página.
  38.         $(window).bind('popstate', function(event) {
  39.             var state = event.originalEvent.state;
  40.             if (state) {
  41.                 // Mostrar url.
  42.                 alert(state.path);
  43.             }
  44.         });
  45.     }
  46.    
  47.     function hash(){
  48.         // Para i.e
  49.         // Función para determinar cuando cambia el hash de la página.
  50.         $(window).bind("hashchange",function(){
  51.             var hash = ""+window.location.hash+"";
  52.             hash = hash.replace("#!","")
  53.             if(hash && hash != ""){
  54.                 alert(hash);
  55.             }
  56.         });
  57.         // Evento al hacer click.
  58.         $("a").bind('click', function(e) {
  59.             e.preventDefault();
  60.             var url = $(this).attr('href');
  61.             // Cambio el historial del navegador.
  62.             window.location.hash = "#!"+url;
  63.             //$(window).trigger("hashchange");
  64.             return false
  65.         });
  66.     }
  67.     </script>  
  68.   </head>
  69.   <body>
  70.     <a href="page-help.html">help</a>
  71.     <a href="other.html"> Otro link</a>
  72.   </body>
  73. </html>

Cita:
Iniciado por JinRoh Ver Mensaje
Hola, me gustaria saber si es posible cambiar la URL del navegador mediante javascript (sin refrescar la página).

Llevo una página de videos, en el cual los videos se seleccionan mediante javascript, pero me gustaria que al hacer click en, por ejemplo, el enlace javascript para el video 20 , la URL se cambiara a BASE/?video=20 (que también es un enlace válido, pero utilizo el javascript por que no me gusta que se refresque la página)

Alguna idea de como hacerlo? O alguna forma de ir a ?video=XX sin refrescar la página? Tenia una idea de , refrescar la página y luego ir al "anchor" del reproductor, para que casi no se notase el refresco, pero aun no encontré una solución válida (forosdelweb.com/showthread.php?t=463995)


Saludos y gracias