Anyone here like compressing thier html

Discussion in 'HTML & Website Design' started by clinton, Nov 21, 2007.

  1. #1
    Hey, I was just wondering if there was anyone here who likes to compress html code, what I mean is filtering out line breaks and tabs.

    What I like to do is have the code neat and indented before I parse it so it looks like this:

    
    <table>
         <tr>
              <td>
                   Dog
              </td>
         </tr>
    </table>
    
    HTML:
    and then after it gets parsed the browser source code looks like this, prety much the entire html document, head and all(Since I don't give two flying shits what the code looks like in the browser source code, only what it looks like in my workspace file) This cuts the document size almost in half:

    
    <table><tr><td>Dog</td></tr></table>
    
    HTML:
    What I do is this:
    
    function compress($buffer)
         {
         $getrid = array("\n","\r", "     ");
         return str_replace($getrid,"",$buffer);
         }
    ob_start("compress");
    //html here
    ob_end_flush();
    
    PHP:
    works really good.
     
    clinton, Nov 21, 2007 IP
  2. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Certainly wouldnt do it at run time - why add server load for something that is quick and easy to do yourself?

    As we almost exclusively develop .Net dynamic sites and with how .Net works with controls there is typically little actual real HTML in our projects to compress. Personally would rather see the output HTML laid out fairly neatly than add a relatively small amount of bandwidth by using carriage returns.
     
    AstarothSolutions, Nov 21, 2007 IP
  3. Dan Schulz

    Dan Schulz Peon

    Messages:
    6,032
    Likes Received:
    436
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Nope - it makes the HTML code harder to read. Besides, when using clean, minimal, semantic and valid markup with the CSS and scripting files separated from the Web page (and called externally), the "savings" from compressing the HTML (or other files) would be minimal at best. If you really want to optimize and minimize your file sizes, look into optimizing your images, especially those called via CSS.
     
    Dan Schulz, Nov 22, 2007 IP
  4. clinton

    clinton Well-Known Member

    Messages:
    2,166
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    110
    #4
    good point, if we minimize as much html code as posible and substitute it for css(or classes) so the css can be used over and over again than lots of file size is being spared in the long run.

    example

    
    <table cellspacing="0" cellpadding="0" bgcolor="white" border="0" align="center">
    <tr>
    <td valign="top" align="center" bgcolor="grey">
    <b>
    Header
    </b>
    </td>
    </tr>
    <tr>
    <td valign="top" align="center" bgcolor="blue">
    <p>
    text yo
    </p>
    </td>
    </tr>
    </table>
    HTML:
    vs

    
    <table class="Box">
    <tr>
    <td class="BoxHeader">
    Header
    </td>
    </tr>
    <tr>
    <td> <!-- use child elements -->
    text here yo...........
    </td>
    </tr>
    </table>
    HTML:
    The way I see it is
    any thing that is used once is an id/template elements...
    anything that is used over and over should be a class/text styles etc.

    <body id="Main">

    <div class="Error">
    Error: invalid submit button:p
    </div>
     
    clinton, Nov 22, 2007 IP
  5. Dan Schulz

    Dan Schulz Peon

    Messages:
    6,032
    Likes Received:
    436
    Best Answers:
    0
    Trophy Points:
    0
    #5
    In which case I'd just remove the layout tables and use clean, minimal, semantic and valid HTML instead, separating all the structure from the presentation and behavior, and use external stylesheets and scripts for my CSS and JavaScript files.
     
    Dan Schulz, Nov 22, 2007 IP
  6. JagNet

    JagNet Peon

    Messages:
    18
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Have you considered using gzip compression instead? The file size will be much smaller than by simply removing whitespace.
     
    JagNet, Nov 23, 2007 IP
  7. ::Mike::

    ::Mike:: Peon

    Messages:
    826
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Their isn't any point, how many dialup connections do you get accessing your site?
    The only thing that slows down my site is the damn servers.
     
    ::Mike::, Nov 23, 2007 IP
  8. twistedspikes

    twistedspikes Notable Member

    Messages:
    5,694
    Likes Received:
    293
    Best Answers:
    0
    Trophy Points:
    280
    #8
    420 so far this month (out of 14000 odd visitors)

    So yes it does matter to some people :p

    But I don't compress code like that, image optimisation + clean code works well enough.
     
    twistedspikes, Nov 23, 2007 IP
  9. JagNet

    JagNet Peon

    Messages:
    18
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Even on a fast internet connection the effects of content compression are noticeable, that's why I compress all files including Javascript, CSS, and XML.

    If your server struggles to keep up with the amount of traffic you're getting, then yes I agree that compressing content on the fly isn't a good idea. But for most sites, server speed isn't such an issue.
     
    JagNet, Nov 23, 2007 IP
  10. Dan Schulz

    Dan Schulz Peon

    Messages:
    6,032
    Likes Received:
    436
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I happen to be one of those dialup users.
     
    Dan Schulz, Nov 23, 2007 IP
  11. bonzay

    bonzay Peon

    Messages:
    54
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Don't compress on-the-fly. Compress on deployment. Use Apache Ant or Maven or whatever in order to compress HTML, CSS, JS BEFORE deployment!
     
    bonzay, Nov 23, 2007 IP
  12. ::Mike::

    ::Mike:: Peon

    Messages:
    826
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I'm a dial up user but compressing html really doesn't make a significant difference, unless its a stupid size.
    Your viewers will be spending more time loading outside sources like adsense etc.
     
    ::Mike::, Nov 24, 2007 IP