SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Founder of Primal Skill Ltd. feketegy's Avatar
    Join Date
    Aug 2006
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Server send only javascript when needed

    Is it a good practice to send only specific javascript files from server side language to the browser only when the specific page needs it?

    For ex. in PHP echoing a <script type="javascript" src"...
    tag if a particular page is requested?

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It seems sensible... if the Javascript isn't going to be used, why make the browser download it (unless previously cached) and parse it for nothing?

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,139
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    unused files

    Good practice? Yes. Important? It depends on how much you want to optimize your pages. IMHO it's more important to minimize uneccessary HTTP requests than it is to limit file size. For example, if my CSS file contains style for a form that only appears on one page, I've "wasted" a few bytes. But if every page requests the javascript validation script for that form, every page except the form page is making a wasted HTTP request. Of course, caching usually limits the impact to the first page the user loads, which could be very important with impatient users.

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the consequence is, that you segment your script into a lot of smaller scripts, it may backfire. There's a considerable amount of overhead with each request made, so three files, with the size of 30K take a lot longer to load than one file with a size of 90K. Because the client can cache the javascript file, it can therefore be better to stuff all the javascript used on the entire site into a single file.

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Building on what kyberfabrikken mentioned about making lots of requests, you might find this useful.

  6. #6
    Founder of Primal Skill Ltd. feketegy's Avatar
    Join Date
    Aug 2006
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. I understand that smaller javascript/CSS files cause multiple HTTP requests which is bad.

    But what if I use a PHP script to check which javascript/CSS files are needed for the particular page. Pack those files in one big javascript/CSS file, compress it,etc. and include it with a <script> tag.

    It seems logical to me to do it this way, but I'd like to see your opinions on this subject.

    And what about caching those files? Is it possible? Because the PHP script checks and compress.. the files everytime, no?

  7. #7
    Founder of Primal Skill Ltd. feketegy's Avatar
    Join Date
    Aug 2006
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So is it good?

  8. #8
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by feketegy
    But what if I use a PHP script to check which javascript/CSS files are needed for the particular page. Pack those files in one big javascript/CSS file, compress it,etc. and include it with a <script> tag.
    If you'd read the link I posted, you'd see that it is a good idea and that a lot of people seem to think it's a good idea.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •