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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:15:20+00:00 2026-06-02T02:15:20+00:00

I found this code in the <head> section of an HTML page (a collegue

  • 0

I found this code in the <head> section of an HTML page (a collegue made this but he doesn’t work here anymore):

(function(window, PhotoSwipe){ 
    document.addEventListener('DOMContentLoaded', function(){
        var options = {},
            instance = PhotoSwipe.attach( window.document.querySelectorAll('#Gallery a'), options );
    }, false);
}(window, window.Code.PhotoSwipe));

While I can understand the central part (from document.addEventListener), I can’t understand the first and the last line. What they’re doing here? The code is from an open source image gallery called PhotoSwipe. Any pointer is appreciated.

[EDIT]

Is this code the same as:

document.addEventListener('DOMContentLoaded', function(){
        var options = {},
            instance = window.Code.PhotoSwipe.attach( window.document.querySelectorAll('#Gallery a'), options );
    }, false);

?

  • 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-02T02:15:22+00:00Added an answer on June 2, 2026 at 2:15 am

    This is a self-executing protected bit of code. Let’s break it down:

    (function(window, PhotoSwipe){
      ...
    }(window, window.Code.PhotoSwipe));
    

    The parenthesis cause our code to be executed on its own, without anything else invoking it.

    This creates references to window and window.Code.PhotoSwipe that cannot be tampered with by outside code. So within our parens, PhotoSwipe is a protected alias of window.Code.PhotoSwipe. And window, though the name doesn’t differ, is also a protected reference to the external global window object.

    The next line, the addEventListener line, could be rewritten to bring its anonymous function out as a named function:

    function myFunc() {
      var options = {},
          instance = PhotoSwipe.attach( window.document.querySelectorAll('#Gallery a'), options );
    }
    document.addEventListener('DOMContentLoaded', myFunc, false);
    

    Note, this is functionally the same thing you have in your original code, only we’ve pulled the function out of the addEventListener call and gave it a name.

    addEventListener attaches a callback function to handler certain events. In this case, we’re handling the event DOMContentLoaded. This event is being listened for on the document object. Anytime this event is heard, we respond by calling myFunc.

    The last parameter, false, deals with capturing and bubbling. These are two methods events propagate throughout the DOM. When Capturing, events move from the top of the DOM inward. When Bubbling, they move from the inside of the DOM outward. Using false states you want to handle this in its bubbling phrase.

    Within myFunct, which is called anytime the DOMContentLoaded event happens on the document, we have one line of code which first declares a new object called options. This object is empty, having no members.

    Secondly, you are passing in two arguments to the attach method of the PhotoSwipe object. The first method is a selector. What is does it searches the DOM for elements that match #Gallery a, meaning any anchor element inside an element having the ID “Gallery”. That would mean all of the following:

    <div id="Gallery"><a href="#">Foo</a></div>
    

    Or

    <div id="Gallery">
      <div class="picture">
        <a href="#">Open</a>
      </div>
      <div class="picture">
        <a href="#">Open</a>
      </div>
    </div>
    

    This is associated with the empty object that we created. What PhotoSwipe does internally is unknown at this point, since that code is not presented here.

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

Sidebar

Related Questions

I found this code from here: http://www.cssportal.com/form-elements/text-box.htm But the problem is you can still
Sorry just found this code here - http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html and the mutex was explained with
I found this code for alphanumeric check (Letters, numbers, spaces or underscores) but I
I found this code here class Usable; class Usable_lock { friend class Usable; private:
I found this code posted on SO to change text links into hyperlinks: function
Code: $(ul.menu_body li:even).addClass(alt); $('li a.menu_head').hover(function () { $('ul.menu_body').slideToggle('medium'); }, function(){ $('ul.menu_body').slideToggle('medium'); }); found this
Using h:outputStylesheet I can embed CSS resources in the HTML head section, but how
Using jQuery 1.5.1 Tried this block of code in: head section as well as
I found this code sample on one of the questions; window.onerror = function (msg,
I have found this piece of code in a book: void DeleteList(element *head) {

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.