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 7625551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:12:26+00:00 2026-05-31T05:12:26+00:00

I have lots of functions and event handlers that are split across multiple javascript

  • 0

I have lots of functions and event handlers that are split across multiple javascript files which are included on different pages throughout my site.

For performance reasons I want to combine all of those files into 1 file that is global across the site.

The problem is I will have event handlers called on elements that won’t necessarily exist and same function names.

This is an example of a typical javascript file…

$(document).ready(function(){
    $('#blah').keypress(function(e){
        if (e.which == 13) {
            checkMap();
            return false;
        }
    });
});

function checkMap() {
    // code
}

function loadMap() {
    // code
}

I would need to seperate this code into an object that is called on that specific page.

My thoughts are I could re-write it like this:

(function($) {
    $.homepage = {
        checkMap: function(){
            // code
        },
        loadMap: function(){
            //code  
        }
    };
})(jQuery);

And then on the page that requires it I could call $.homepage.checkMap() etc.

But then how would I declare event handlers like document.ready without containing it in it’s own function?

  • 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-31T05:12:27+00:00Added an answer on May 31, 2026 at 5:12 am

    I think that all you need is a namespace for you application. A namespace is a simple JSON object that could look like this:

    var myApp = {
        homepage : {
          showHeader : function(){},
          hideHeader : function(){},
          animationDelay : 3400,
          start : function(){} // the function that start the entire homepage logic
        },
        about : {
        .... 
        }
    }
    

    You can split it in more files:

    1. MyApp will contain the myApp = { } object, maybe with some useful utilities like object.create or what have you.
    2. Homepage.js will contain myApp.homepage = { … } with all the methods of your homepage page.
    3. The list goes on and on with the rest of the pages.

    Think of it as packages. You don’t need to use $ as the main object.

     <script src="myapp.js"></script>
     <script src="homepage.js"></script>
     <-....->
     <script>
       myApp.homepage.start();
     </script>
    

    Would be the way I would use the homepage object.

    When compressing with YUI, you should have:

    <script src="scripts.min.js"></script>
    <script>
        myApp.homepage.start();
     </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an external JavaScript file that will be used on pages with lots
I have to use an extern library providing lots of free functions that do
I have lots of unrelated named things that I'd like to do quick searches
We have lots of stored procedures which all contain a lot of comments. Is
I have a method in a class for which they are a few different
In my application I have lots of different data types, e.g. Car, Bicycle, Person,
I have been tasked with improving the current mess that is our JavaScript strategy;
I have an action column in my grid which is needed to perform lots
I have a multiview iPhone app I'm developing that will have lots of inputs
This Javascript which uses functions from jQuery is quite handy but getting feedback on

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.