HTML question: "including" chunks of code?

Discussion in 'Web Design and Development' started by Super Macho Man, Oct 25, 2006.

  1. Super Macho Man macrumors 6502a

    Super Macho Man

    Joined:
    Jul 24, 2006
    Location:
    Hollywood, CA
    #1
    Sorry if this is an obvious question. I don't know where to look for an answer or what this might be called. My last experience with HTML was from the HTML 3.0 days. :D

    Let's say I have a few static HTML pages on a site. They are all supposed to have the same "look." They all link to the same CSS file, and I've designed them similarly etc. But I am wondering if it is possible to define common "code chunks" and just reference these chunks from each individual page. E.g. so that if I change a chunk, I change every page that uses that chunk. It would sure beat going through 8 different HTML files to adjust some tiny little property in my site-wide footer every time I want to do that.
     
  2. cbetta macrumors regular

    Joined:
    Jul 5, 2006
    #2
    use a dynamic language for this, like php, asp, jsp etc. What you do i just include your chuncks where you need them.
     
  3. Nermal Moderator

    Nermal

    Staff Member

    Joined:
    Dec 7, 2002
    Location:
    New Zealand
    #3
    I don't know of a pure HTML way of doing that, but you could put your chunks into .js files, using this kind of structure:

    Code:
    document.write('<table><tr><td>My Chunk</td></tr></table>');
    document.write('<img src="hello.jpeg" alt="Hi" />');
    etc.
    Then reference them with:

    Code:
    <script language="javascript" type="text/javascript" src="mychunk.js" />
    Note that this is from memory so there may be a couple of syntax errors :)
     
  4. MrSmith macrumors 68040

    MrSmith

    Joined:
    Nov 27, 2003
    #4
    Javascript would be troublesome as you'd need to put document.write etc. around every line. With PHP you can write one sheet and refer to it from other sheets. For example, write the common code in common.php. Then just write:

    <?php
    include ('common.php');
    ?>

    in the html page wherever you want the common code.

    PHP is great because you can just pop in and out of it on a html page. Like this:

    <some html>
    <?php
    some php code
    ?>
    <back to html>

    The server needs to have PHP on it, of course.
     
  5. dalvin200 macrumors 68040

    Joined:
    Mar 24, 2006
    Location:
    Nottingham, UK
    #5
    agreed.. server side coding is way better than using client side javascript for the common code.
     
  6. c-Row macrumors 65816

    c-Row

    Joined:
    Jan 10, 2006
    Location:
    Germany
    #6
    Keep in mind that there are still some users who turn off JavaScript. So you should either keep important things out of the js. files (like a site navigation - would be bad if that was missing) or provide stripped down alternates within the <noscript> tag which are likely not to change as regularly.

    The number of no-JS users gets smaller all the time, but still... ;)
     
  7. ChicoWeb macrumors 65816

    ChicoWeb

    Joined:
    Aug 16, 2004
    Location:
    California
    #7
    You can use .shtml as well. In case you don't want to use PHP or ASP. I guess it depends on your server. I think .php or .asp looks more profesh, but I guess it depends what you are looking to acomplish.
     
  8. Super Macho Man thread starter macrumors 6502a

    Super Macho Man

    Joined:
    Jul 24, 2006
    Location:
    Hollywood, CA
    #8
    That is exactly what I was looking for. I tried it and it works beautifully. Thanks for all the help!
     
  9. MrSmith macrumors 68040

    MrSmith

    Joined:
    Nov 27, 2003
    #9
    That good feeling when something works... :) Glad it helped.
     

Share This Page