Forum Moderators: open

Message Too Old, No Replies

compressing code

any drawbacks

         

click watcher

12:52 pm on Mar 24, 2002 (gmt 0)



have been running into bandwidth problems on one site,

and have found that by closing all the gaps in my code, (except where scripting requires new lines!!!) i have cut down file sizes considerable and can live with the downside which is that when editing it becomes harder to see whats what.

but are there any other drawbacks from a spider point of view (or any other for that matter)

pageoneresults

1:22 pm on Mar 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ah-ha, I can appreciate a topic on optimizing html! One of the first things I look at is what can I put in an external file?

1. All JavaScript
2. All CSS

That in itself will shrink your html considerably. Removing all spaces, tabs, etc. is another step and yes, if you are not used to the format, it can be difficult to locate things. If I ever run into that issue, I insert comments and a white space above the comment tag so when I scan the html I can see where the section breaks are.

I always tell my clients that if you view the source code of a web page and your browser tells you that the file is too big for Note Pad and requires Word Pad to open it, then it is too big!

P.S. I've seen no drawbacks in performing the above steps. If there are any, the advantages far outweigh the disadvantages.

Macguru

1:41 pm on Mar 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use external external JS and CSS files to improve "content\fluff ratio", but I dont see how it can help with bandwidth problems.

buckworks

1:41 pm on Mar 24, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I usually try to do that and have not noticed any ill effects. I have pages that are #1 in Google for their target terms.

Things you do to save bandwidth will usually make your pages faster too, which improves your visitor's experience.

Something else I did was to convert some recurring elements to external .js files, to take advantage of client-side caching. Example: I have a search box on every page and a large drop-down menu. I moved both of those to external .js files. The first page that gets visited will not save any bandwidth that way, but as visitors move around the site those elements are already in the browser cache so subsequent pages will be leaner and faster. Also (still learning here) I'm using CSS to eliminate tons of font tags etc. and using an external CSS file.

Optimize all graphics (I find the GIFbot at NetMechanic to be helpful here), and be cautious about using a graphic if text can do the same job (for instance, in menus).

Macguru

1:44 pm on Mar 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So external JS and CSS get stored in local disc cache?

buckworks

1:46 pm on Mar 24, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



That's my understanding, yes.

pageoneresults

1:48 pm on Mar 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I was always under the impression that everything got stored in users cache. I've been using includes for years for just that reason. When a user visits any page of the site, the includes, the js and the css are all loaded into the users cache. And as mentioned by buckworks, all pages that use those files are quick to load because any content from those files is already loaded in their cache.

pageoneresults

1:51 pm on Mar 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Macguru, I was involved in another discussion elsewhere and the topic of calling external files was brought up. Apparently, depending on the speed of your hosting server, external files can sometimes create a slight delay.

The reason this came up is because I made a suggestion to someone to place all their css and js in external files. They replied stating that the hosting server loaded the pages slower when they had external files. Their host told them it was because of the server speed and the number of sites that server had on it.

I haven't had to worry about that issue yet, don't think that I ever really will unless I change hosts. We have about 220 sites on that server, not enough to blink an eye at!

Macguru

1:54 pm on Mar 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



After all that time data mining logs and optimising pages, I didn't even think of that! I was still on the idea that those files where called everytime a page was loaded. I never looked into my cached files about it before.

That is what I like about this forum, what is obvious for one is a discovery for one other. I keep on learning everyday. Thanks for sharing it pageoneresults and buckworks!

buckworks

2:01 pm on Mar 24, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Make sure you're not confusing server-side includes with .js external files. They're separate animals. SSI includes have to be processed new for every page and yes they will slow down the host server a bit. They have major advantages for site maintenance but not for speeding up pages.

pageoneresults

2:06 pm on Mar 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



SSI includes have to be processed new for every page and yes they will slow down the host server a bit.

buckworks, my includes contain images and such. Are you saying that those images are reloaded each time a page is visited that uses that include? Or is it just the calling of the file itself? My understanding is that each time an image is loaded into users cache, that it won't load again unless they clear history.

click watcher

2:07 pm on Mar 24, 2002 (gmt 0)



yes thanks everyone,

been using external css and js for a while which is definately a great help.
and they are surely cached so when you use the same styles over and over again only one call is required.

pageoneresults

2:09 pm on Mar 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



click_watcher, the next thing I would do is take a close look at your code and see if there is anything else that can go into your external css. I'll assume that you've rid yourself of the <font> tag. If not, you can create classes to replace those which will cut back on code bloat.

The next thing would be to rid yourself of additional tables that are not required, if you are using them. Empty tags, broken tags, etc. are all candidates for compressing your html.

sun818

5:01 am on Mar 25, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is this what I need to place on my web page to save CSS and JS files to the local HD?

<LINK REL=STYLESHEET HREF="style.css" TYPE="text/css">

<SCRIPT SRC="javascript.js"></SCRIPT>

ggrot

6:13 am on Mar 25, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



External files can actually be slower loading. The reason is that for every distinct file that is downloaded from the server, the client(web browser) has to perform this little chat with the server (content negotiation, file request, cookie exchange) about the file it wishes to get first. If the files is very small (a 2 line css file for example), this chat may be significantly more bandwidth than the file itself. In this case, depending on the number of average pageviews, you might want to leave certain content in the html file. Secondly, when a file request comes into a server, it puts it onto a wait queue that handles all other fiels requests FIRST. So it may be a few split seconds before the new file begins to download. This shouldn't be a serious problem for your users or make any difference in your bandwidth, but might explain your hosting company's response.

Other possibilities of reducing bandwidth are using:
1) site-wide graphics
2) smaller variable/form names
3) server side file compression
4) hand coded html/javascript

madmatt69

11:50 am on Mar 25, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi guys,

I also put all my js and css in external files, I find it works great. I use Dreamweaver most of the time, take the javascript it creates and make a seperate file which all of my html pages call (if they need the js of course!). I have also found that sometimes the server downloads the external files last or slowest or something, and for about half a second my page doesn't have a background or something, but generally it works really well. Definetely recommend doing it.

Eric_Jarvis

12:16 pm on Mar 25, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



using site wide graphics is a great bandwidth saver...also if you are using an old style spacer gif layout, make sure you are using a single pixel transparent gif...there is no need to make a whole load of different spacers

better still shift to a div/css layout

cfel2000

1:12 pm on Mar 25, 2002 (gmt 0)

10+ Year Member



When I use CSS in order to call a custom class I use <font class="Name"> can I just use <class="Name> or something else?

tedster

1:26 pm on Mar 25, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The standard way to attach a "custom" class to some place in the body copy would be a <p> tag or a <div> -- even a <b> or <li> or <a> -- almost any element. If you're totally out of options in some spot you can use a <span>.

But the <font> tag has been deprecated. Although it's not dead yet, it won't be around indefinitely. Since CSS was created in part to get font tags out of the markup, it's interesting to me that browsers even execute a class on a font.

Macguru

2:41 pm on Mar 25, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



sun818
>>what I need to place on my web page to save CSS and JS files to the local HD?
I think this is the proper way to call an external JS so HTML validate.

<script type="text/javascript" language="JavaScript" src="javascript.js"></script>

So what would be a good rule of thumb for external JS files calls? Put all those Netcape bug fix, sniffers and rollover scripts in the same file usually makes a 4 k file. Is it worth it?

madmatt69

4:26 pm on Mar 25, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Macguru, I know what you mean - But would you rather have that 4k in an external file that gets called once, or 4k of code cluttering up every single html doc? What I do is depending on the site. If the js is needed on each page, then it makes sense to put it all in an exteral js file. 4k over 10 pages is 40k..when you could just have an extra 4k downloaded on the first page that works for all the rest of the pages.

But if I only have rollovers on certain pages, then I put the rollover script in it's own file (eg. rollover.js) and only call it on the pages where it's necessary.

BlobFisk

6:22 pm on Mar 25, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On the HTML compression note, there is a handy little programme that removes unnecessary whitespace from html documents called Absolute HTML Compressor.

You can download this (free) from [serpik.com ].

HTH

BlobFisk

IanKelley

9:51 am on Mar 27, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To the first post... line breaks are not required in scripting... that's what ;'s are for. You can compress your javascript and CSS just as much as you compress your HTML.

Thing is though... if you write your HTML code and scripts by hand you'll find that stripping tabs, spaces and line breaks will only save you a few % points. It's almost pointless. If your HTML is bloated it's because you're using a WYSIWYG editor.

About external js and CSS... to settle the debate :-) they save both bandwidth and time.

No js and CSS do not slow down the server. Only SSI can do that.

SSI is not cached. It is exactly as if the code were included directly in the HTML page.

External files are checked every page load, not just the first one. No one mentioned that one yet. The bandwidth savings happens because every time after the first it's only the fraction of a second it takes to inform the broswer that the file hasn't changed.

Another small savings (if you're hosted on a good server and have your external calls before any image preloading) happens because, even on a dial up connection, the first second of straight text will usually transmit at 8-20 K/second (sometimes more) before packet loss starts to kick in.