Can you see a problem with the DOM ready method?

Discussion in 'Javascript' started by adambones, Dec 7, 2009.

  1. adambones

    adambones Guest

    I am using the following code in my applications for kick-starting
    JavaScript applications:

    function App(d) {
    this.d = d; this.n = d.createElement('div');
    }
    App.prototype.run = function() {
    try {
    this.d.body.appendChild(this.n);
    this.d.body.removeChild(this.n);
    f();
    } catch (e) { setTimeout(arguments.callee, 50) }
    }

    // new App(document).run(function() { alert('Lets go') })

    The problem I've had with using DOMContentLoaded is that you must be
    sure that the document is *not* ready at the time you attach the event
    - otherwise it will never fire, and in the case of FF which has no
    readyState property, you cannot check that a document is already
    loaded.

    This issue is arising for me since my application requires me to check
    the ready state of an iframed document from a parent window. In some
    cases the framed document is already loaded by the time the parent
    window code runs.

    I'm posting this here because I'd appreciate if anyone can point out
    any pitfalls in this approach. It's seems way too simple and obvious
    to me, which leads me to fear I am missing something...
    adambones, Dec 7, 2009
    #1
    1. Advertising

  2. adambones

    adambones Guest

    Sorry, I posted that code without running it. It should be something
    like:

    function run(d, f) {
    var n = d.createElement('div');
    (function() {
    try {
    d.body.appendChild(n);
    d.body.removeChild(n);
    f();
    } catch (e) { setTimeout(arguments.callee, 50) }
    })()

    You get the idea anyway...
    adambones, Dec 7, 2009
    #2
    1. Advertising

  3. adambones

    David Mark Guest

    On Dec 7, 8:00 am, adambones <> wrote:
    > Sorry, I posted that code without running it. It should be something
    > like:
    >
    >     function run(d, f) {
    >       var n = d.createElement('div');
    >       (function() {
    >         try {
    >           d.body.appendChild(n);
    >           d.body.removeChild(n);
    >           f();
    >         } catch (e) { setTimeout(arguments.callee, 50) }
    >       })()
    >
    > You get the idea anyway...


    Not really. Is this an assumption that you can catch "Operation
    Aborted" errors? You might be able to in IE8 from what I've heard
    (never tried), but IE6/7 users will see nothing but a generic error
    page. And an append success is not an indication the document is
    finished loading and safe for scripting.

    The solution is to have the framed document call a predetermined
    global "ready" function (or method of an object) provided by the
    parent frame.
    David Mark, Dec 7, 2009
    #3
  4. adambones

    adambones Guest

    > You might be able to in IE8 from what I've heard
    > (never tried), but IE6/7 users will see nothing but a generic error
    > page


    I'm not sure what you mean - where is an error page coming from?
    Either f is run or it isn't (and I'm running it in IE6+).

    > And an append success is not an indication the document is
    > finished loading and safe for scripting.


    This is really what I want to know - if it is *not* an indication that
    can you shed some light on why or give an example case?

    > The solution is to have the framed document call a predetermined
    > global "ready" function (or method of an object) provided by the
    > parent frame.


    In my case I need the child window to be agnostic of the parent. The
    application is a CMS - the top window is the editing interface, and
    the child can be any website.
    adambones, Dec 7, 2009
    #4
  5. adambones

    David Mark Guest

    On Dec 7, 9:23 am, adambones <> wrote:
    >  > You might be able to in IE8 from what I've heard
    >
    > > (never tried), but IE6/7 users will see nothing but a generic error
    > > page

    >
    > I'm not sure what you mean - where is an error page coming from?
    > Either f is run or it isn't (and I'm running it in IE6+).


    You are trying to insert elements into the body of a document, but you
    have no idea what state it is in. The IE catastrophe I referred to is
    known to be sporadic (as you might expect with something like this).
    Google for IE and "Operation Aborted".

    >
    > > And an append success is not an indication the document is
    > > finished loading and safe for scripting.

    >
    > This is really what I want to know - if it is *not* an indication that
    > can you shed some light on why or give an example case?


    It is not an indication because it may succeed while the document is
    still parsing (and therefore dangerous to manipulate). The only thing
    holding it back is when the body hasn't started parsing (e.g. script
    running in the head), document.body is null. That's the error you're
    catching. As soon as the body starts parsing, your code will succeed
    in many browsers (even sporadically in IE).


    >
    > > The solution is to have the framed document call a predetermined
    > > global "ready" function (or method of an object) provided by the
    > > parent frame.

    >
    > In my case I need the child window to be agnostic of the parent. The
    > application is a CMS - the top window is the editing interface, and
    > the child can be any website.


    If the child can be any Website, how do you plan to manipulate it with
    script?
    David Mark, Dec 7, 2009
    #5
  6. adambones wrote:

    > I am using the following code in my applications for kick-starting
    > JavaScript applications:
    > [...]
    > I'm posting this here because I'd appreciate if anyone can point out
    > any pitfalls in this approach. It's seems way too simple and obvious
    > to me, which leads me to fear I am missing something...


    What do users do without script support? What are search robots going to
    find there?


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
    Thomas 'PointedEars' Lahn, Dec 7, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Flip
    Replies:
    3
    Views:
    972
    Tony Morris
    Feb 9, 2004
  2. Joenco
    Replies:
    2
    Views:
    745
    Joenco
    Jul 3, 2003
  3. Steve Kershaw
    Replies:
    1
    Views:
    356
    Brennan Stehling
    Sep 26, 2006
  4. Replies:
    0
    Views:
    289
  5. Replies:
    12
    Views:
    223
    Thomas 'PointedEars' Lahn
    Feb 27, 2008
Loading...

Share This Page