Buscando asesoramiento sobre si es posible tener un footer.html y duplicarlo en todas las páginas sin usar PHP.
Sé que puedo usar include("footer.php"), pero ahora todas mis páginas están en HTML y tendré que cambiar todas las rutas CSS.
Leí w3school sobre cómo usar w3-include-html y javascript. Sin embargo, me doy cuenta de que el javascript se está retrasando en cada página, lo que hace que la carga de la página sea más larga.
¿Hay alguna otra opción?
<body> <script> function includeHTML() { var z, i, elmnt, file, xhttp; /* Loop through a collection of all HTML elements: */ z = document.getElementsByTagName("*"); for (i = 0; i < z.length; i++) { elmnt = z[i]; /*search for elements with a certain atrribute:*/ file = elmnt.getAttribute("w3-include-html"); if (file) { /* Make an HTTP request using the attribute value as the file name: */ xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4) { if (this.status == 200) {elmnt.innerHTML = this.responseText;} if (this.status == 404) {elmnt.innerHTML = "Page not found.";} /* Remove the attribute, and call this function once more: */ elmnt.removeAttribute("w3-include-html"); includeHTML(); } } xhttp.open("GET", file, true); xhttp.send(); /* Exit the function: */ return; } } } </script> <div w3-include-html="footer.html"></div> <script> includeHTML(); </script>Podrías pensar diferente, pero no sé si es lo que quieres, porque la arquitectura de tu aplicación crecerá/se volverá un poco fea.
Usted define un script php, que está cargando su html estático y agrega el código php que desea. Entonces necesita una regla de reescritura para que el script sea el centro de su pequeño universo html ;-)
Un ejemplo, que debería funcionar en un apache con rewrite-mod habilitado.
Mencione que crea esta estructura de directorio en su servidor:
Coloque un .htaccess en la carpeta raíz:
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond "%{REQUEST_URI}" !^.*/index\.php$ RewriteRule ^(?!.*index\.php)(.*)$ /index.php/$1 [QSA,L]Coloque un index.php en la misma carpeta:
<?php ... here could be your header-code define ('PAGES_DIR', __DIR__ . '/html-pages') define ('DEFAULT_PAGE', 'index.html') // we need to handle virtual pathes $requestPath = false; if (isset($_SERVER['PATH_INFO'])) { $requestPath = $_SERVER['PATH_INFO']; } elseif (isset($_SERVER['ORIG_PATH_INFO']) && strpos($_SERVER['ORIG_PATH_INFO'], basename(__FILE__)) === FALSE) { $requestPath = $_SERVER['ORIG_PATH_INFO']; } if ($requestPath == '/' || strlen(trim($requestPath)) == 0 || strlen(trim($requestPath)) == basename(__FILE__)) { $requestPath = DEFAULT_PAGE; } // this sends the content of your file to the browser @readfile(PAGES_DIR . "/$requestPath"); ... now your footer-codeY sus archivos html deben colocarse en la carpeta "páginas html".
Si lo ha hecho, su servidor debe manejar http://domain.tld/bla-fasel-blub.html de la siguiente manera:
Espero que te sirva, generando una idea para tu problema.