views:

125

answers:

3

A slew of pages I've written for one of my web projects share some 144 identical lines of code, reproduced in each file. If I update one of those lines, I have to go back through ALL of the pages that share the code and update for each page. Is there a straightforward way to include HTML from a separate file?

And for bonus points, since so many pages use this code, it would be nice not to have to reload it for each page. Is there an easy way to store it in the browser's cache or only load the "content" section of the pages while leaving the rest of the page static?

Fountains of Thanks for any wisdom on this.

Mike

+1  A: 

If we're assuming that you're talking straight html pages, with no server code (asp.net, php, or server side include ability), then in order to do both the including and the caching, you're going to need to use an iframe.

Each of your pages that duplicate the 144 lines of content should replace it with an iframe like so:

<iframe src="pagewithcontent.html"></iframe>

pagewithcontent.html would obviously be where you move the content to. The browser will cache the page appropriately, so that each parent page will simply get the shared content without making another request.

There's an article here that goes into great depth about html includes, and some javascript methods of doing it. I would strongly recommend against the javascript methods.

My answer reflects the assumption that you can't do anything on the server side. However, by far the best solution is to do so if you can.

womp
You'd approach server-side includes with more caution than throwing iframes around all willy-nilly and messing up your DOM?
Chuck
@Chuck I think `womp` is referring to the javascript XHR based including described on the linked article, which I would avoid like bad cliches. Still, iframes are not even close to being a good answer for this problem.
garrow
Given no ability to do it on the server, what's your answer? I'm interested to hear it.
womp
If you can't do it server-side (which is very unusual these days, but whatever), you can do it statically before pushing it to the server. There are many static site generators out there.
Chuck
That would require republishing the entire site every single time you changed the content, and doesn't meet the caching requirement posed in the question.
womp
I have no experience with SSI, but that may be an option. I do have a server. I am far more familiar with client-side development, so the iFrame seems like a good short-term solution. If I wanted to use SSI, how do I start? When I go to my server, I see an interface called cPanel X VPS Optimized 2. I pretty much don't know how to do anything here ;). Thanks.
Mike
I'm not sure what that interface would be. Are you using a hosting provider? SSI depends on what web server you are using - either Apache or Internet Information Services (IIS). You could test to see if SSI is enabled on your webserver just by adding an SSI directive into your page as a test and see if it works. Otherwise, I suggest starting at wikipedia: http://en.wikipedia.org/wiki/Server_Side_Includes.
womp
+3  A: 

I would recommend reading this article on includes

ichiban
I felt this article was better after reading both: http://www.yourhtmlsource.com/sitemanagement/includes.html#enablingdirectory
Mike
+2  A: 

To include HTML from a separate file, use SSI (Server-Side Includes). This requires SSI support to be installed on the server, however.

You would write something like this in your files: <!--#include file="included.html" --> and that would include the file included.html when the page is accessed.

To load only the content of each page, use the XMLHTTPRequest object from JavaScript:

function LoadContent(url)
{
    if (typeof(XMLHttpRequest) == "undefined")
    {
        try
        {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e)
            {
                // fallback for browsers without XMLHttpRequest
                window.location.href = "no-ajax.php?url="+escape(url);
                return;
            }
        }
    }
    else
    {
        xmlhttp = new XMLHttpRequest();
    }
    xmlhttp.open("GET", url, false); // this request will be synchronous (will pause the script)
    xmlhttp.send();
    if(xmlhttp.status > 399) // 1xx, 2xx and 3xx status codes are not errors
    {
        // put error handling here
    }
    document.getElementById("content").innerHTML = xmlhttp.responseText;
}
immibis
Excellent answer. This is the route I took. Some clarification on the XMLHTTPRequest part: Where do I call this function? Do I need to adjust it in any way?
Mike
You would want to add error handling where it says to - and either replace the line:`window.location.href = "no-ajax.php?url="+escape(url);`with more error handling code if the browser doesn't support XMLHttpRequest, or create no-ajax.php so it loads the whole page.
immibis