viernes 7 de julio de 2006

"Includes" con JS usando XML

Muchas veces tenemos paginas en servidores que no soportan los Server Side Includes, con este script podemos agregar una pagina como un include pero siendo Client Side Include :-D .

NOTA: Algunos servidores, sobretodo gratuitos marcan un error con este script, por lo que no se garantiza que a todos les sirva

Para instalar el script coloca lo siguiente entre las etiquetas <head></head>

<script type="text/javascript">
function include(xUrl,xId) {

var xmlhttp = false;

/*@cc_on @*/

/*@if (@_jscript_version >= 5)

// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.

try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}

/*@end @*/

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}

xmlhttp.open("GET", xUrl,true);
xmlhttp.onreadystatechange=function() {

if (xmlhttp.readyState==4) {
document.getElementById(xId).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null)

}
</script>



Y ahora llamamos la funcion asi:
<div id="'xjam"></div>
<script type="text/javascript">
include('contenido.htm', "xjam");
</script>


Donde cambiaremos el archivo a incluir que es lo marcado en negritas

Etiquetas: , , , , , ,


Ver tema completo