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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:24:20+00:00 2026-06-07T00:24:20+00:00

I am loading two JS files, one with many plugins, including Jquery. The ‘ready()’

  • 0

I am loading two JS files, one with many plugins, including Jquery. The ‘ready()’ JQuery function is in the second file.

Code:

   function downloadJSAtOnload() {

       var element = document.createElement("script");
       element.src ='<%= deferJsAll %>';
       document.body.appendChild(element);

      // second JS file load with isReady goes here
   }

   // Check for browser support of event handling capability
   if (window.addEventListener)
       window.addEventListener("load", downloadJSAtOnload, false);
   else if (window.attachEvent)
       window.attachEvent("onload", downloadJSAtOnload);
   else window.onload = downloadJSAtOnload;

You can see when I also load the second file (See comment inside the code).

The problem is that I can’t control the order of execution and even so, the ‘ready()’ runs before JQuery is loaded, and I get an error. Is there an option to use defer loading and making sure that the Jquery files loads first and then the second is executed (the one with the ready() function)?

I am using “<%= deferJsAll %>” because I am deciding which file to serve from CDN, the gzip or plain text JS file.

Progress: I thought about just putting:

var elementApp = document.createElement("script");
elementApp.src = 'js/app.js';
document.body.appendChild(elementApp);

at the end of the first document. So when the first document finishes loading, it will then embed the second dependent JS code into the body of the document. Do you think it’s a good idea?

  • 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-06-07T00:24:21+00:00Added an answer on June 7, 2026 at 12:24 am

    Here’s a solution I used when I need to wait until some var on the page exists. It takes a name (e.g. jQuery or namespace twttr.widgets with arbitrary depth) and other self explanatory attributes. Works in production on http://www.vitalmtb.com/ when I need to wait till Facebook and Twitter widgets are loaded.

    // Wait till JS object is loaded. It can be a chanined variable name.
    window.whenAvailable = function (name, callback, interval, max_wait) {
        interval || (interval = 50); // ms
        max_wait || (max_wait = 5000); // ms
        var timer,
            findVarByName = function(name) {
            var index = 0,
                parts = name.split('.'),
                result = window;
    
            index = 0;
            try {
                while (typeof result !== "undefined" && result !== null && index < parts.length) {
                    result = result[parts[index++]];
                }
            }
            catch (e) {}
            if (index < parts.length || (typeof result == 'undefined' && result === undefined)) return false;
            else return true;
        };
    
        timer = window.setTimeout(function() {
            if (findVarByName(name)) {
                return callback();
            } else {
                window.setTimeout(arguments.callee, interval);
            }
        }, interval);
    
        window.setTimeout(function() {
            clearTimeout(timer);
        }, max_wait);
    }
    

    Use:

    whenAvailable('FB', $j.spotlights.a.show_social_buttons);
    whenAvailable('twttr.widgets', $j.spotlights.b.show_social_buttons);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A the end of my index.html file I'm including two script files. One of
When loading two CSS files via an include I am only seeing one of
I am trying to run two scripts: one for loading page content using ajax
I have two kml files. One contains polygons for counties, and the other pins
How does one combine two GCC compiled .o object files into a third .o
I have two sets of identical html and css files. One of which I'm
I have two excel files. First excel file contains the Person Name and Total
I'm working with OPE IDs. One file has them with two trailing zeros, eg,
im loading files inside two divs at the same time: #ajax_id and #ajax_col, the
I have two projects, one project loads files from the project folder, one project

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.