0

I have a series of large html pages in development, each uses a common structure: header / content / sidebar.

Header and sidebar sections have code common to all pages. Only content changes.

During the development phase, I want to be able to make changes to the header and sidebar once and see the results replicated on all pages without having to edit each page separately.

I've done a bit of googling, and the simplest solution seems to be to create 2 separate text files with the code for the header and sidebar sections, and then inject this at the appropriate points in the HTML pages I'm editing.

I know this is not recommended for a live project, as it would dramatically increase the load times, but for dev work it would save me a lot of time.

Would you agree? If so, anybody have any idea what the simplest way to do this is?

Thanks

4
  • I think you can user AJAX to update only the body or any other div of the page, Commented Jul 10, 2013 at 15:12
  • 1
    api.jquery.com/load I think this is what you want. EDIT: Sorry this should have been a comment. Commented Jul 10, 2013 at 15:12
  • 1
    Any reason you want to use javascript only for this?, Your server side language should be able to include all the files you require Commented Jul 10, 2013 at 15:13
  • Why don't you use a static site generator like Jekyll? jekyllrb.com Commented Jul 10, 2013 at 15:13

3 Answers 3

6

You would be better to do this with some sort of server-side technology, like PHP. This would make the impact on loading times negligible as the pages would be concatenated before they were sent to the browser.

You would accomplish it by doing something like this:

<?php require('header.php'); ?>

<!-- Main content goes here --> 

<?php require('sidebar.php'); ?>

And then have the files header.php and sidebar.php in the same directory as the main files.

Sign up to request clarification or add additional context in comments.

Comments

0

Use some sort of serverside templating languate that would allow for you to include files within each other.

Comments

0

If, despite the other answers, you insist on doing this from JavaScript, the following is a module I use :

(function xload (slctr) { //================================================== xload ===
  function xloader (src, dst) {
    if (arguments.length == 1) {
      dst = src;
      src = dst.getAttribute ('data-source') || '';
    }

    var req;

    try {
      src = src.match (/^([^]*?)(!)?(?:\[\[([^]*?)\]\])?$/);
      (req = new XMLHttpRequest ()).open ('GET', src[1], !src[2]);
      req.onreadystatechange = function (ev) {
        if (this.readyState === 4) {
          if (typeof dst == 'function')
            dst (req);
          else {
            dst[dst.tagName == 'TEXTAREA' ? 'value' : 'innerHTML'] = this.responseText;
            [].forEach.call (dst.getElementsByTagName ('SCRIPT'), function (s, i) {
              var script = document.createElement ('script');
              script.innerHTML = s.parentNode.removeChild (s).innerHTML;
              document.body.appendChild (script);
            })
          }
        }
      };
      src[3] && req.overrideMimeType &&
        req.overrideMimeType (src[3]);
      req.send (null);
    } catch (err) { console.log ('xloader error : ', err); }
  }

  [].forEach.call (document.querySelectorAll (slctr), function (el) { xloader (el); });

}) ('[data-source]'); //------------------------------------------------------ xload ---

Any element, a div for example with a data-source attribute is processed. The data-source specifies the url of the file to be included. When he Ajax request completes, the entire contents of the div are replaced with the text fetched. The data-sourc url may optionally be followed by a ! indicating synchronous load and then by a Mime type enclosed in [[ and ]]

Any scripts in the loaded text are extracted and injected into the document body.

Error conditions are reported on the console.

The module is entirely standalone and processes all elements containing the data-source attribute. It should, of course be loaded after the HTML text of the page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.