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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:36:48+00:00 2026-05-27T19:36:48+00:00

i want to load an array of page-specific external .js files. in the past,

  • 0

i want to load an array of page-specific external .js files. in the past, i did it through PHP at the top of each page, like so:

<?php
  $jsFiles = array("file01.js", "file02.js", ...);
  include("header.php");
?>

the header.php file loaded the files like so:

foreach ($jsFiles as $file) {
  echo "<script type='text/javascript' src='_js/$file'></script> \n";
}

but now i need to do this all in JS because i have to load these files AFTER domready which is checked and fired in the header.php file…

this is the idea of what i want and i KNOW IT IS NOT CORRECT CODE, so rein in the comments! it is a conceptual construct.

for (file in jsFiles) {
  console.log('loading ' + file);
  Asset.javascript(file);
};

and, yup, i read this and this; close, but not quite.

questions i have are:

1) how to create the arrays on a per-page basis

2) how to load that array properly using the Asset class

thanks.

WR!

  • 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-27T19:36:49+00:00Added an answer on May 27, 2026 at 7:36 pm

    how you create the arrays is up to you. you can define an array with an array literal like so:

    var filesToLoad = ["foo.js", "bar.js"];
    // or even from a flat string...
    var files = "foo.js,bar.js",
        filesToLoad = foo.split(",");
    

    you can also setup a global js structure based upon something that identifies what page you are on, eg:

    var pageAssets = {};
    pageAssets["*"] = ["common.js"];
    pageAssets["home"] = ["foo.js","bar.js"];
    pageAssets["about"] = ["foo.js","bar.js","about.js"];
    
    // in domready determine what to load...
    var id = document.id("body").get("id");
    
    // see down for explanation...
    Asset.javascripts(pageAssets[id], {
        onComplete: function() {
            alert("all js loaded");
        }
    });
    

    if the order of loading then does not matter, you can use plain Asset.javascript as described:

    filesToLoad.each(function(file) {
        new Asset.javascript(file);
    });
    

    the downside to all that is that this is async lazyloading. meaning that if you run a line immediately underneath that instantiates something based upon what you think you loaded, it will likely fail unless you’re on a lan with primed cache.

    I have extended Asset.js to support this:

    Asset.javascripts = function(sources, options) {
        // load an array of js dependencies and fire events as it walks through
        options = Object.merge({
            onComplete: Function.from,
            onProgress: Function.from
        }, options);
        var counter = 0, todo = sources.length;
    
        var loadNext = function() {
            if (sources[0])
                source = sources[0];
    
            Asset.javascript(source, {
                onload: function() {
                    counter++;
                    options.onProgress.call(this, counter, source);
                    sources.erase(source);
    
                    if (counter == todo)
                        options.onComplete.call(this, counter);
                    else
                        loadNext();
                }
            });
        };
    
        loadNext();
    };
    

    you just pass on the array as the sources argument and can set an onComplete and onProgress events. this also ensures FIFO from the array so if your dependency order matters, this will be fine.

    the example i wrote / orig blog post is here: http://fragged.org/lazyloading-multiple-sequential-javascript-dependencies-in-mootools_1389.html

    you should also read up about Require.js / AMD to make things more resilient from a dependency standpoint.

    have fun.

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

Sidebar

Related Questions

EDIT: I want to load the page for each gamertag, so that it creates
This plugin reads image files on blueimproot/server/php/files on page load. I need to read
I want to load data from a MySQL database to a PHP page using
I have a class - XClass, that I want to load into an array
I want to load up a random image while a page it is loading
I want a web page to display several random images on load. I've thought
I have a page that I want to load and display for an amount
I want to have a paged layout where each page is a fragment with
I have a text array. I want to display the first entry on page
I have a list of URLs, each page is a specific category: http://www.site.com/category-1/page.html http://www.site.com/category-2/page.html

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.