Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [287]

By Root 1495 0
“).load(“head.html“);

$(“#menu“).load(“menu.html“);

$(“#content1“).load(“story1.html“);

$(“#content2“).load(“story2.html“);

$(“#footer“).load(“footer.html“);

};

//]]>

id = ”content1”>

id = ”content2”>

Look over the code, and you can see these interesting features:

♦ The page has no content! All the divs are empty. None of the text shown in the screen shot is present in this document, but all is pulled from smaller files dynamically.

♦ The page consists of empty named divs. Rather than any particular content, the page consists of placeholders with IDs.

♦ It uses jQuery. The jQuery library is used to vastly simplify loading data through AJAX calls.

♦ All contents are in separate files. Look through the directory, and you can see very simple HTML files that contain small parts of the page. For example, story1.html looks like this:

Book I - Creating the XHTML Foundation

  1. Sound XHTML Foundations
  2. It’s All About Validation
  3. Choosing your Tools
  4. Managing Information with Lists and Tables
  5. Making Connections with Links
  6. Adding Images
  7. Creating forms
  8. ♦ The init() method runs on document.ready. When the document is ready, the page runs the init() method.

    ♦ The init() method uses AJAX calls to dynamically load content. It’s nothing more than a series of jQuery load() methods.

    This approach may seem like a lot of work, but it has some very interesting characteristics:

    ♦ If you’re building a large site with several pages, you usually want to design the visual appearance once and reuse the same general template repeatedly.

    ♦ Also, you’ll probably have some elements (such as the menu and heading) which that will be consistent over several pages. You could simply create a default document and copy and paste it for each page, but this approach gets messy. What happens if you have created 100 pages according to a template and then need to add something to the menu or change the header? You need to make the change on 100 different pages.

    The advantage of the template-style approach is code reuse. Just like the use of an external style allows you to multiply a style sheet across hundreds of documents, designing a template without content allows you to store code snippets in smaller files and reuse them. All 100 pages point to the same menu file, so if you want to change the menu, you change one file and everything changes with it.

    Here’s how you use this sort of approach:

    1. Create a single template for your entire site.

    Build basic HTML and CSS to manage the overall look and feel for your entire site. Don’t worry about content yet. Just build placeholders for all the components of your page. Be sure to give each element an ID and write the CSS to get things positioned as you want.

    2. Add jQuery support.

    Make a link to the jQuery library, and make a default init() method. Put in code to handle populating those parts of the page that will always be consistent. (I use the template shown here exactly as it is.)

    3. Duplicate the template.

    After you have a sense of how the template will work, make a copy for each page of your site.

    4. Customize each page by changing the init() function.

    The only part of the template that changes is the init() function. All your pages will be identical, except they have customized init() functions that load different content.

    5. Load custom content into the divs with AJAX.

    Use the init() function to load content into each div. Build more content as small files

Return Main Page Previous Page Next Page

®Online Book Reader