Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 325569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:13:44+00:00 2026-05-12T09:13:44+00:00

How do you guys organise your javascript code? I know that it is good

  • 0

How do you guys organise your javascript code? I know that it is good practice to store the code in an external .js file and this is fine for code that is run in multiple pages but how do you organise if you have, say 20 pages, and only 1 of them uses a particular function. Do you create a new external file for that 1 page or create the code inline?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:13:44+00:00Added an answer on May 12, 2026 at 9:13 am

    I do two things:

    1. I put all my site’s Javascript in one or more files;
    2. I include that file or files on every page that uses any Javascript;
    3. Those files are cached effectively such that they are only ever downloaded once (until they change); and
    4. Pages call the functions they need from those external files.

    If your site is one page then put it inline.

    If your site is 20 pages and they all use a little bit of Javascript, put it all in one files, include it on every page and call the functions with inlien Javascript as necessary in each file.

    I write about this and more in Supercharging Javascript in PHP. Sure it’s PHP-specific but the principles are universal.

    Basically every extra HTTP request is a problem. So if you have 20 pages each with a different Javascript file then that’s a problem, even if those files are small. It’s better to combine all that Javascript in one file, download it just once (with effective caching) and just use what you need.

    To give you an example. External JS file contains:

    function delete_user(evt) { ...}
    function suspend_user(evt) { ... }
    function unsuspend_user(evt) { ... }
    

    One of your Web pages contains:

    $(function() {
      $("#delete").click(delete_user);
      $("#suspend").click(suspend_user);
      $("#unsuspend").click(unsuspend_user);
    });
    

    This way you get an external JS that contains all your site’s Javascript but none of it is actually used. Use comes from inline code in the pages. This way there is no overhead of having the larger JS file.

    Whatever you do, don’t put in ALL initialization in your Javascript file. I once made this mistake and put a huge $(function() { … } function into the external file on the grounds that if the relevant IDs weren’t in the page, nothing would happen. There ended up being enough of this code to add nearly half a second to the page load time (and the site wasn’t that big).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 192k
  • Answers 192k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would keep the original entered mess but would also… May 12, 2026 at 6:28 pm
  • Editorial Team
    Editorial Team added an answer Well, you don't need to create a separate directory and… May 12, 2026 at 6:28 pm
  • Editorial Team
    Editorial Team added an answer 1) Yes, but it would be more precise and accurate… May 12, 2026 at 6:28 pm

Related Questions

How do you guys organise your asp.net web applications? Do you have you classes
I've been running some metrics on my Java project and apparently there are a
Is there an easy way to create a cross platform portable Xulrunner application? By
How do you guys manage the information overflow? What are the tools that you

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.