Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>First, why would DiggWidget ever be a function?</p> </blockquote> <p>It will become a function when the asynchronous script loads and executes</p> <blockquote> <p>Is DiggWidget (the array) available to that .js</p> </blockquote> <p>Yes, <code>window.DiggWidget</code> is global so it's available to that script.</p> <p>The way the widget works is rather simple. </p> <p>If the <code>DiggWidget</code> script has not been loaded yet, then <code>window.DiggWidget</code> will not be a function. Initially it will be an undefined variable so the else block </p> <pre><code>} else { DiggWidget = [diggWidget]; </code></pre> <p>executes and defines it as an array. From now onwards and until the widget script loads, it will be defined as an array.</p> <p>Now until the <code>DiggWidget</code> script loads asynchronously, keep pushing all initialization objects <code>{..}</code> to an array of the same name - <code>window.DiggWidget</code>.</p> <p>When the script is loaded, before it overtakes the global <code>DiggWidget</code> 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.</p> <p>Here's the full embed code annotated with comments.</p> <h3>User code</h3> <pre><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); } })(); </code></pre> <h3>Widget code (at the top)</h3> <pre><code>(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 } } </code></pre> <h3>(at the bottom)</h3> <pre><code> // 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()) } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload