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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:20:09+00:00 2026-05-28T01:20:09+00:00

I have a situation where I’m trying to consolidate several Javascript files into one,

  • 0

I have a situation where I’m trying to consolidate several Javascript files into one, and conditionally apply them. I don’t want to modify the actual files. Is there a way I can wrap these files and call them on demand?

The catch is, some of these files have function xyz() {} in them. So, wrapping them with if (false) { function xyz() {} } makes bad things happen.

For example, if this is my code…

if (includeFile) {
    /* The file */
    function foo() {}
    /* The file */
}

The problem becomes that Chrome will see foo() and place it in the global scope even if includeFile is false.

The easy solution would be to modify it to be var foo = function() {} but I can’t modify these files.

I also have a general concern about running eval() on these functions since they are fairly huge. (Think jQuery wrapped in a function. If this isn’t a problem then maybe eval is the answer?)

I was hoping I could nest functions and pass window in as the scope, but tried it on jsFiddle and it didn’t seem to work.

(function() {
    function foo() {
        alert('it works');
    }
}).apply(window, []);

foo();

There are a few similar questions. But, none addressed the same situation that I have. Thanks.

  • 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-28T01:20:10+00:00Added an answer on May 28, 2026 at 1:20 am

    Have you considered using a library like require.js? There are several libraries out there that can do this for you in a more elegant fashion.

    If you don’t want a dependency-loading library, but you’re using jQuery or some similar library, you can load scripts conditionally using an AJAX request (in jQuery, you’d use the script dataType). This is far better than a simple eval(), but less robust when you’re trying to manage a series of dependencies.

    Another option here, if you want to simply concatenate everything, is to wrap each file in an anonymous function and then assign the necessary elements to the window object, placing them in the global scope:

    (function(window) {
    
        /* file 1 here */
        function xyz() {
            // etc ...
        }
        /* end file 1 */
    
        // now selectively choose what you want to be
        // in the global scope
        window.xyz = xyz;
    
    }(window));
    
    xyz();
    

    This requires more work for you, however, to identify what you want to be available globally. Note that it’s note necessary to pass window in as an argument to the anonymous function, but it’s not a bad idea if you’re going to be be referring to it multiple times (this speeds up references and allows for variable-name munging during code compression).

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

Sidebar

Related Questions

I have situation, where I want to read configuration file only one time, when
Possible Duplicate: What is my script src URL? I have situation: <script type=text/javascript src=http://server/some.js>
We have a situation where one of our products is a 32-bit app, but
I have this situation where i want to call a certain element depended on
I have this situation that when AbstractMethod method is invoked from ImplementClass I want
I have a situation where I want to create a signature of a data
I have a situation where another developer is including source files from a project
I have situation when one of tables rich size 2G and after it I
I have situation where I want to change layout of activity after 3 seconds.
I have situation where I want to extract multiple values from multiple source objects

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.