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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:10:22+00:00 2026-05-28T18:10:22+00:00

I typically put all the JavaScript scripts into one file e.g. scripts.js (the less

  • 0

I typically put all the JavaScript scripts into one file e.g. scripts.js (the less HTTP request, the better). So, as expected, some scripts are needed for some pages, some aren’t.

To target a specific page, I use something like:

if ($("body#share").length > 0) {
    // Place the logic pertaining to the page with ID 'share' here...
}

// The file / script continues...

Other or better suggestions? Thanks!

Clarification: I was not looking for the pros / cons between consolidating multiple JS files into one big file and keeping multiple separate JS files. The answer for this is surely ‘depends on the situation’ (we know that). My question is, assuming all my JS logic is placed into one big file, how do I make a particular (chunk of) script runs only when the corresponding page is loaded? One way I used to do is using if ($('#id-of-target-element')) { /* run the script */}; is there a better way?

  • 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-28T18:10:23+00:00Added an answer on May 28, 2026 at 6:10 pm

    I like Paul Irish’s approach… you don’t have to follow it exactly, but the general idea is a very solid one.

    It might look something like this for your example

    Html

    <body id="share">
    

    Your page specific javascript

    YourNamespace = {
      share : {
        init : function(){
          // Place the logic pertaining to the page with ID 'share' here...
        }
      }
    }
    

    Paul Irish’s Javascript that makes the magic happen

    UTIL = { 
      fire : function(func,funcname, args){
        var namespace = YourNamespace;  // indicate your obj literal namespace here
    
        funcname = (funcname === undefined) ? 'init' : funcname;
        if (func !== '' && namespace[func] && typeof namespace[func][funcname] == 'function'){
          namespace[func][funcname](args);
        }
      }, 
    
      loadEvents : function(){
        var bodyId = document.body.id;
    
        // hit up common first.
        UTIL.fire('common');
    
        // do all the classes too.
        $.each(document.body.className.split(/\s+/),function(i,classnm){
          UTIL.fire(classnm);
          UTIL.fire(classnm,bodyId);
        });
    
        UTIL.fire('common','finalize');
      }
    };
    
    // kick it all off here 
    $(document).ready(UTIL.loadEvents);
    

    So the line you see directly above will kick off the following

    YourNamespace.common.init()
    YourNamespace.share.init()
    YourNamespace.common.finalize()
    

    Have a read of his blog post and a few of the variations linked from it.

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

Sidebar

Related Questions

I prefer to have one workspace and put all my projects into this one
My request is that a client wants to put in a date range (typically
While I'm learning a new language, I'll typically put lots of silly println's to
Why should I use DTOs/Domain objects when I can just put all business classes
I have a multi-module (maven) spring build. All the modules publish some beans, and
i have the following string: http://pastebin.com/d29ae565b i need to separate each value and put
I use PrimeFaces 3.0-SNAPSHOT with JBoss AS7. I put three <p:fileUpload> tags in one
Copying a File using a straight-forward approach in Python is typically like this: def
I have a database of tables that I needed to do some comparison work
Typically I develop my websites on trunk, then merge changes to a testing branch

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.