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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:53:44+00:00 2026-05-16T17:53:44+00:00

I am building a javascript widget that should load asynchronously. Problem is that there

  • 0

I am building a javascript widget that should load asynchronously.

Problem is that there can be more than 1 of these widgets on the page and that the widget should be initialized by sending it an option array via {}.

What is the best way to accomplish this? I heard that simply setting onload or onreadystatechange does not work in all browsers.

I’ve checked out the digg widget, but I can’t really comprehend what they’re doing, could anyone take a look at that?

Here’s some of their code:

(function () {
var s, s1, diggWidget = {
    id: "digg-widget-1282651415272",
    width: 300,
    display: "tabbed"
};
if (window.DiggWidget) {
    if (typeof DiggWidget == 'function') {
        new DiggWidget(diggWidget);
    } else {
        DiggWidget.push(diggWidget);
    }
} else {
    DiggWidget = [diggWidget];
    s = document.createElement('SCRIPT');
    s.type = 'text/javascript';
    s.async = true;
    s.src = 'http://widgets.digg.com/widgets.js';
    s1 = document.getElementsByTagName('SCRIPT')[0];
    s1.parentNode.insertBefore(s, s1);
}
})();

So, if the DiggWidget is already available (loaded earlier due to multiple instances), it makes a new widget if DiggWidget is a function, otherwise DiggWidget is used as an array and the current settings are pushed to it.

First, why would DiggWidget ever be a function?

If the widget is the only one (or first), the script tag is added asynchronously, no callbacks are defined.

Then, looking at widgets.js they do this:

At the top:

(function () {
var A;
if (window.DiggWidget) {
    if (typeof DiggWidget != "function") {
        A = DiggWidget
    } else {
        return
    }
}

At the bottom:

    if (A) {
    while (A.length) {
        new DiggWidget(A.shift())
    }
}

Now I don’t quite understand this. Is DiggWidget (the array) available to that .js ? It’s in a anonymous function. So if I include such a script twice, wouldn’t DiggWidget be a new instance every time?

Or am I completely in the wrong on this? Sorry if so. If there’s any better methods to have a callback with multiple instances of the script, please do tell.

  • 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-16T17:53:45+00:00Added an answer on May 16, 2026 at 5:53 pm

    First, why would DiggWidget ever be a function?

    It will become a function when the asynchronous script loads and executes

    Is DiggWidget (the array) available to that .js

    Yes, window.DiggWidget is global so it’s available to that script.

    The way the widget works is rather simple.

    If the DiggWidget script has not been loaded yet, then window.DiggWidget will not be a function. Initially it will be an undefined variable so the else block

    } else {
        DiggWidget = [diggWidget];
    

    executes and defines it as an array. From now onwards and until the widget script loads, it will be defined as an array.

    Now until the DiggWidget script loads asynchronously, keep pushing all initialization objects {..} to an array of the same name – window.DiggWidget.

    When the script is loaded, before it overtakes the global DiggWidget variable it sees the objects in that array, and safely records it somewhere else. Then takes over the DiggWidget name, loops through each object, and initializes the widget for each.

    Here’s the full embed code annotated with comments.

    User code

    (function () {
    var s, s1, diggWidget = {
        id: "digg-widget-1282651415272",
        width: 300,
        display: "tabbed"
    };
    // either the external widget script has already loaded, or there is more than
    // one widget to be embedded on the page, and the previous widget's embed code
    // defined this variable
    if (window.DiggWidget) {
        // the external widget script has been loaded asynchronously 
        // because DiggWidget is a function. Can directly create a new object.
        if (typeof DiggWidget == 'function') {
            new DiggWidget(diggWidget);
        }
        // a previous widget's embed code defined this global array. Remember the
        // widgets initialization settings for when the external script loads.
        else {
            DiggWidget.push(diggWidget);
        }
    }
    // the external widget script has not loaded yet and
    // this is the first widget ever to be embedded on the page
    else {
        // DiggWidget does not exist at this point. So make it an array
        // and store the first widget's config object in it
        DiggWidget = [diggWidget];
        s = document.createElement('SCRIPT');
        s.type = 'text/javascript';
        s.async = true;
        s.src = 'http://widgets.digg.com/widgets.js';
        s1 = document.getElementsByTagName('SCRIPT')[0];
        s1.parentNode.insertBefore(s, s1);
    }
    })();
    

    Widget code (at the top)

    (function () {
    var A;
    // global DiggWiget is defined. it will either be an array if 
    // the widget code hasn't loaded yet, or a function if it has
    if (window.DiggWidget) {
        // it's an array, so keep that array somewhere safe
        // because we are gonna take over the DiggWidget variable purdy soon
        if (typeof DiggWidget != "function") {
            A = DiggWidget
        }
        // window.DiggWidget must be a function
        // there is no widget to initialize, just return
        else {
            return
        }
    }
    

    (at the bottom)

        // A is the array of widget settings we backed up a little earlier
        if (A) {
        // loop through each config object and create the actual
        // widget object
        while (A.length) {
            new DiggWidget(A.shift())
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a javascript-powered gallery on the iPad that can display both images and
I'm currently researching the best way to approach building a JavaScript widget someone can
I have an SVG I am building with Javascript. I load in a large
I'm building a non-javascript version of a website, as there are a number of
I'm building a concert calendar that's very heavy on javascript (using jQuery). I'm having
I am building a small widget that I am giving to users to embed
I'm building an interactive javascript application that needs to make some SOAP requests to
I am looking to create a web widget that can be easily integrated into
I'm building a JavaScript parser and there seem to be some differing opinions. ECMA-262
I am building Javascript application for mobile browsers (not wrapped-as-native app). I noticed that

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.