SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Function wrapper for addEvelentListener

    Hello,

    Code JavaScript:
    window.addEventListener( "DOMContentLoaded", function() {
              //...
            }, false);

    I'd like to wrap this bit of code in a function:

    Code JavaScript:
    var onReady = function()
        {
     
            window.addEventListener( "DOMContentLoaded", function() {
              //...
            }, false);
     
        }

    I'd like then to be able to use the ready as follow:

    Code JavaScript:
    onReady( 
     //do stuff here
    );

    Obviously, this is not working. I've been searching for functions wrapper for addEventListener but didn't come up with anythings satisfying. What would be the correct approach/syntax to wrap an eventListener in a function, and then use that function instead of the event listener?

    Thanks in advance.


  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    From what i can gather you are wanting to run the onReady function when the DOMContentLoaded event is triggered, see the below.

    Code JavaScript:
    var onReady = function(event) {
        // Do something here...
    };
     
    window.addEventListener('DOMContentLoaded', onReady, false);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •