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

  • Home
  • SEARCH
  • 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 7758833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:28:07+00:00 2026-06-01T13:28:07+00:00

Check out this piece of JavaScript code: (function (w, d) { var loader =

  • 0

Check out this piece of JavaScript code:

(function (w, d) {
    var loader = function () {
        var s = d.createElement("script"), tag = d.getElementsByTagName("script")[0];
        s.src = "https://example.org/script.js";
        tag.parentNode.insertBefore(s,tag);
    };
    w.addEventListener ? w.addEventListener("load", loader, false) :
                         w.attachEvent("onload", loader);
}) (window, document);

Why did the author of this code use this method to include a script in the document?
And what is the usefulness of the line:

w.addEventListener ? w.addEventListener("load", loader, false) :
                     w.attachEvent("onload", loader);

Last point: I’m a JavaScript beginner, what is the (window, document) at the end?

  • 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-01T13:28:09+00:00Added an answer on June 1, 2026 at 1:28 pm

    The following addEventListener line is to register the function so that it gets called when the page finishes loading. Internet Explorer and Firefox (et al) use different functions to do this.

    w.addEventListener ? w.addEventListener("load", loader, false) :
                     w.attachEvent("onload", loader);
    

    In javascript a function is an object in and of itself. As such it’s ‘value’ can be assigned to a variable or consumed immediately. To consume it immediately it must be wrapped in parentheses (otherwise it won’t do what you want) and then call it like it were a regular function.

    (function (w, d) { ... }) (window, document);
    

    It’s more obvious what is going on if we break it up across two lines.

    var a = function(w, d){ ... };
    a(window, document);
    

    It was done this way to as to not pollute the global scope with temporary values or functions. Not to mention not trashing anyone else variables. This can be broken into two parts:

    1. By encapsulating the code in a closure anything explicitly declared inside is in the closure’s scope, not the global scope. var loader is in the closure’s scope.
    2. By consuming the closure immediately it won’t be stored in a variable in the global scope. Since it was declared anonymously it won’t exist as a named function in the global scope.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys. Check out this piece of sample code. #include stdafx.h #include<conio.h> #include<string.h> class
Please check out this piece of code, more specifically the hourStep calculations. int h
We have this piece of code: <a class=loadbutton onClick=$('checkout_form').submit(); $(this).update('<img src=\'images/loader/longLoad.gif\' /> Processing'); return
First check out this code. I seems like it should work for me, but
Please check this code out it compiles and runs absolutely fine.. The question is
I use the following piece of code to check out and then check in
Can some one please explain to me why this piece of code prints out
I am not able to make out whatrs wrong with this piece of code
Check out this test: [TestFixture] public class Quick_test { [Test] public void Test() {
Update: Check out this follow-up question: Gem Update on Windows - is it broken?

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.