Un recurso para ajax, rápido y fácil

Comments   0   Date Arrow  Marzo 31, 2008 at 8:55pm   User  by Pedro Marce

Si queréis trabajar con Ajax será mejor que os lo miréis con calma y veáis como funciona, pero si queréis algo rápido para dotar de algo de funcionalidad Ajax a vuestra aplicación, miraros este código.
function callasync (url, id_div)
{
var page_requested = false;
if (window.XMLHttpRequest)
{
// If Mozilla, Safari etc
page_requested = new XMLHttpRequest ();
} else if (window.ActiveXObject)
{
// but if IE
try
{
page_requested = new ActiveXObject ("Msxml2.XMLHTTP");
}
catch (e)
{
// old version
try
{
page_requested = new ActiveXObject ("Microsoft.XMLHTTP");
}
catch (e)
{
}
}
}
else
return false;
page_requested.onreadystatechange = function ()
{
// función de respuesta
loadedpage (page_requested, id_div);
}
page_requested.open ('GET', url, true); // asignamos los métodos open y send
page_requested.send (null);
}

function loadedpage (page_requested, id_div)
{
if (page_requested.readyState == 4 && (page_requested.status ==
200 || window.location.href.indexOf ("http") == - 1))
document.getElementById (id_div).innerHTML = page_requested.responseText;
var counter = document.getElementById('Counts');
if (counter && UpdateCounter) {
UpdateCounter(counter.value);
}
}

Y después simplemente, ejecuta la función callasync pasándole una URL que (y esto es importante) sólo devuelva el html que va dentro del DIV que ponéis en el segundo parámetro y está definido en vuestra página, y ya tenéis un muy bonito efecto y muy sencillo….

Tagged   javascript · tutorial · utilidades

0 Comments

  • There are no comments yet, be the first by filling in the form below.

Leave a Comment


Cerrar
Enviar por Correo