Problem with document.addEventListener("DOMContentLoaded", initialize, false)


  • Hello,

    I want to load 'inizialize()' function when DOM is ready, but, when I have more than one HTML fragment, 'document.addeventlistener' reference to the main document and doesn't work.

    How can I do to wait only the Dom fragment where I call script?


    Tuesday, November 15, 2011 7:00 PM

All replies

  • Hi Sergi,

    If you create a Grid or ListView application you can see the correct pattern for HTML fragments.  Following this pattern will ensure that you can add the listener after everything is loaded correctly.



    Jeff Sanders (MSFT)
    Wednesday, November 16, 2011 12:51 PM
  • Yes, it's  posibility, but, imagine this example:

    My application has a HTML with 3 buttons, one of them goes to another HTML (HTML2) and, in HTML2 there are a button and an image.

    If I need to get an image with document.getElementById("image"); I can't do it, because 'document' doesn't refer to HTML2, it returns 'NULL'.

    What could I do? Can I do nothing?


    Thank you for your answer!


    Wednesday, November 16, 2011 9:11 PM
  • Hi Sergi,

    You will notice that you can do this using the fragments.  As an example, create a blank SplitView javascript HTML application.  Then open splitPage.js.

    The main document object is available in the fragmentLoad event.  The actual fragment items will be part of the DOM at that point and accessible.  You can also get to the fragment that was loaded by the 'elements' object that was passed into the fragmentLoad event.

    All of this magic happens with the fragmentappended event listener that the WinJS application will fire for you.


    Jeff Sanders (MSFT)
    Wednesday, November 30, 2011 3:36 PM