"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
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: agregar, contenido, include, javascript, server, side, xml
2 Comentarios:
Tio no consigo ponerlo. ¿Podrias agregarme y ayudarme?.
ruben_osa@hotmail.com
Gracias.
5:15 PM
Espero te haya servido lo que te mande por el messenger, y pues si, es problema del servidor gratuito que te agrega codigo al archivo final
4:36 PM
Publicar un comentario en la entrada
Enlace a este tema:
Crear un vínculo
<< Inicio