SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Wizard Goof's Avatar
    Join Date
    Feb 2001
    Location
    Pittsburgh, PA
    Posts
    1,154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best Practice for Page-Specific JavaScript?

    Hey guys,

    I just wanted to get your opinions on this: I have several pages with page-specific JavaScript (i.e. script I know cannot be reused...it can only be run against elements found only on that page).

    My question is, would you recommend just putting it in SCRIPT tags in the header, or should I create thispage.js so that the code is separated into another file? It slows the request because the browser has to ask for another external file, but keeps code out of the HTML doc.

    What are your thoughts? What do you do?
    Nathan Rutman
    A slightly offbeat creative.

  2. #2
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use page specific js as well. The main libray code is loaded globally, and the page specific js is used mainly in implementing library code and manipulating the dom.

    With pages using the following url convention /controller/action/
    I store page specific js in js/controller/{action}.js files.

    Browsers can cache these files, whereas if you dumped the code directly in the header then you would be serving up extra kb's every page load

  3. #3
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree. Always put it in an external file if you can manage it, the browser can cache it and it will result in an overall faster page-load.
    <(^.^<) \(^.^\) (^.^) (/^.^)/ (>^.^)>
    Core 2 Duo E8400 clocked @ 3.375GHz, 2x2GB 800MHz DDR2 RAM
    5x SATA drives totalling 2.5TB, 7900GS KO, 6600GT

  4. #4
    SitePoint Wizard Goof's Avatar
    Join Date
    Feb 2001
    Location
    Pittsburgh, PA
    Posts
    1,154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, but if the JS is ONLY used in 1 document, it can cache the HTML file too. And downloading an 11 kb HTML document will be faster than downloading a 8 kb HTML + 3kb JS doc because of the extra request.
    Nathan Rutman
    A slightly offbeat creative.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,870
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The appropriate way to attach ALL JavaScript to your pages is:

    Code:
    <script type="text/javascript" src="generic.js"></script>
    <script type="text/javascript" src="pagespecific.js"></script>
    </head>
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Place it in an external file for a month, make sure you have good logging going on.

    If at the end of that month you're noticing alot of repeat visitors downloading thoose external files multiple times, meaning they clear their caches too often for the benefits of the external files to be had, then put it directly on the page & free up some of the load the external files put on the server.


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
  •