Ver Mensaje Individual
  #6 (permalink)  
Antiguo 15/05/2008, 13:51
Avatar de Panino5001
Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años
Puntos: 834
Re: Problema con recarga en div con ajax

A ver con un ejemplo:
Página 1 (donde se carga todo en una capa de id pp):
Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<
title>test</title>
<
script>
function $(
id){return document.getElementById(id);}
function 
http(){
    if(
window.XMLHttpRequest){
        return new 
XMLHttpRequest();    
    }else{
        try{
            return new 
ActiveXObject('Microsoft.XMLHTTP');
        }catch(
e){
            
alert('nop');
            return 
false;
        }    
    }
}

function 
SetContainerHTML(id_contenedor,responseText)
{
mydiv document.getElementById(id_contenedor);
//reemplazando por code salvamos bug de Explorer 7
responseText=responseText.split('SCRIPT').join('code')
mydiv.innerHTML responseText;
var 
elementos mydiv.getElementsByTagName('code');
for(
i=0;i<elementos.length;i++) {
var 
elemento elementos[i];
nuevoScript document.createElement('script');
nuevoScript.text elemento.innerHTML;
nuevoScript.type 'text/javascript';
if(
elemento.src!=null && elemento.src.length>0)
{
nuevoScript.src elemento.src;}
elemento.parentNode.replaceChild(nuevoScript,elemento);
}
}
function 
cargarPagina(url,contenedorId){
var 
H=http();
H.open('get',url,true);
H.onreadystatechange=function(){
    if(
H.readyState==4){
        
SetContainerHTML(contenedorId,H.responseText);
        
H.onreadystatechange=null;
    }else{
        $(
contenedorId).innerHTML='cargando...';
    }
}
H.send(null);
}
window.onload=function(){
    
cargarPagina('pagina2.php','pp');
}
</script>  
</head>

<body>
<div id="pp"></div>
</body>
</html> 
Pagina 2:
Código PHP:
<?php 
ob_start
('cod');
function 
cod($b){return utf8_encode($b);}
?>
Soy p&aacute;gina 2 <a href="javascript:cargarPagina('pagina3.php','pp')">cargar p&aacute;gina 3 </a>
<?php 
ob_end_flush
();
?>
Pagina 3:
Código PHP:
<?php 
ob_start
('cod');
function 
cod($b){return utf8_encode($b);}
?>
<!-- en mayúscula evitamos bug de explorer 7 -->
<SCRIPT>
function probar(mensaje){alert(mensaje);}
</SCRIPT>
soy página 3 <a href="javascript:probar('hola!')">probar javascript</a>
<?php 
ob_end_flush
();
?>

Última edición por Panino5001; 15/05/2008 a las 22:57