Gracias David este es el codigo
Código javascript
:
Ver original<!-- http://www.lawebdelprogramador.com -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>AJAX: Número aleatorio</title>
<script type="text/javascript">
<!--
var Servidor="aleatorio.php";
function Conectar()
{
if(window.XMLHttpRequest)
Conexion=new XMLHttpRequest(); //mozilla
else if(window.ActiveXObject)
Conexion=new ActiveXObject("Microsoft.XMLHTTP"); //microsoft
}
function Contenido()
{
if(Conexion.readyState!=4) return;
if(Conexion.status==200) // Si conexion HTTP es buena !!!
{
document.getElementById("temp").innerHTML=Conexion.responseText;
}else{
document.getElementById("temp").innerHTML=Conexion.status+"-"+Conexion.statusText;
}
Conexion=false;
}
function Solicitud()
{
if(Conexion) return; // Previene uso repetido del boton.
Conectar();
if(Conexion)
{
Conexion.open("POST",Servidor,true);
Conexion.onreadystatechange=Contenido;
}else
document.getElementById("temp").innerHTML="No disponible";
}
window.onload=function()
{
Solicitud(); // Inicializa el numero aleatorio al cargar la pagina
document.getElementById("boton").onclick=Solicitud;
}
//-->
</script>
</head>
<body>
<p>Código que mediante un botón o enlace permite actualizar un valor aleatoriamente.</p>
Número aleatorio: <span id="temp">No disponible</span> <a href="javascript:void(null);" id="texto">generar</a>
<br /><input type="button" id="boton" value="generar" />
</body>
</html>