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

  • SEARCH
  • Home
  • 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 7630299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:04:08+00:00 2026-05-31T06:04:08+00:00

To keep organized, I’d like to keep all the javascript for my site in

  • 0

To keep organized, I’d like to keep all the javascript for my site in a single file:

scripts.js

However, some of my scripts are only used on on some pages, other scripts are only used on other pages.

In my document-ready function it looks like this:

function home_page() {
  // image rotator with "global" variables I only need on the home page
}

$('#form')... // jQuery form validation on another page 

The problem with this, is that I am getting javascript to execute on pages it’s not even needed. I know there is a better way to organize this but I’m not sure where to start…

  • 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-31T06:04:09+00:00Added an answer on May 31, 2026 at 6:04 am

    I usually do something like this, or some variation (a little pseudo code below) :

    var site = {
        home: {
           init: function() {
               var self=this; //for some reference later, used quite often
               $('somebutton').on('click', do_some_other_function);
               var externalFile=self.myAjax('http://google.com');
           },
           myAjax: function(url) {
               return $.getJSON(url);
           }
        },
        about: {
           init: function() {
               var self=this;
               $('aboutElement').fadeIn(300, function() {
                   self.popup('This is all about me!');
               });
           },
           popup: function(msg) {
               alert(msg);
           }
        }
    };
    $(function() {
        switch($('body').attr('class')) {
           case 'home':
              site.home.init(); 
           break;
           case 'about':
              site.about.init();
           break;
           default: 
              site.error.init(); //or just home etc. depends on the site
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to keep my scripts organized in one .js file for all my
I like to use several different workspaces, so as to keep everything organized. However,
Is it possible to merge several JavaScript files being used on a site into
I've some js files organized this way (see source ): gmaps4rails.base.js : contains all
What is the best way to keep large projects organized with SSMS? I want
I keep hearing that div tags should be used for layout purposes and not
I keep reading about C99 and C++11 and all these totally sweet things that
I'm wanting to detect all objects(classes) defined in a php file (example: flavors.php). The
I am trying to keep a somewhat organized directory structure as I plan to
In our daily work we maintain some rather large Javascript libaries. We use VS2008

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.