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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:59:38+00:00 2026-06-04T05:59:38+00:00

I have an array of URLs of content to load asynchronously, e.g. var toLoad

  • 0

I have an array of URLs of content to load asynchronously, e.g.

var toLoad = ['snippet1.html', 'snippet2.html', ... ];

I’m trying to load each snippet with AJAX and append it to a parent <div>, maintaining the order of content in the array. Because of the way the rest of my code is set up (and for performance reasons), I don’t want to load in a synchronous manner, either with async:false or by chaining .ajax() calls, as suggested in answers to this similar question. Instead, I’d like to put logic in the AJAX callback function to place loaded snippets correctly, based on the index.

The following code works:

var $parent = $('#mydiv');
toLoad.forEach(function(url, i) {
    // load snippet
    $.get(url, function(snippet) {
        // attach order to DOM element
        snippet = $(snippet).data('order', i);
        // find the sibling with the next lowest order number
        var $prev = $parent
            .children()
            .filter(function() {
                return $(this).data('order') < i
            })
            .last();
        if ($prev[0]) snippet.insertAfter($prev); 
        else $parent.prepend(snippet);
    });
});

…but I don’t like it. In particular, I don’t like attaching the order to the DOM element with .data(); this seems ugly, and I’d rather not stick this into the DOM if I can help it.

Is there a better way to achieve the goal of asynchronously loading content into a div with a predefined order?

  • 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-04T05:59:39+00:00Added an answer on June 4, 2026 at 5:59 am

    If your main gripe is using .data(), you could maintain your own array which associates dom node elements to values.

    var map = [
        {order: 5, nodeReference: ...},
        {order: 9, nodeReference: ...},
        {order: 1, nodeReference: ...}
    ];
    function data(node) {
        // loop through map, comparing to nodeReference to get the order 
    }
    

    //and then
    …

        .filter(function() {
            return data(this) < i
        })
    

    your code mostly stays the same, except you need to add to the map when you append something to the dom.

    I would try hard to continue to progressively add things to dom as soon as they load, like you do now. I wouldn’t wait until everything is loaded, or even adding them in order. let the order work itself out eventually like you do now. this is how web browsers load images in web pages, and there’s deep reasons for it.

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

Sidebar

Related Questions

Okay, so I have a JSON array of image URLs. I want to load
I have an array of URLs and I wan't to open each one and
I have an array in PHP with URLs like below: http://example.com/apps/1235554/ http://example.com/apps/apple/ http://example.com/apps/126734 http://example.com/images/a.jpg
I have an array that stores a few urls of XML files that need
Say I have an array of 3 URLs with an iFrame on the page.
I have a script that has the following content: <?php if(isset($_POST['sent-urls']) || $_POST['sent-urls'] ==
I have an array of Image URLS .I want to dynamically add Pivot item
I am trying to create a content management system with SEO friendly URLs ..
I have an array with urls, like: [1] = http://site.com/1.pdf [2] = http://site.com/234234234.png [3]
I have a bunch of urls like these. $urls = array( 'https://site1.com', 'https://www.site2.com', 'http://www.site3.com',

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.