SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2005
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Outside the DOM?

    I've recently run across a problem which I think I understand, but could do with clarification from the experts on here...

    I've been using Jeremy Keith's addLoadEvent to attach functions to the onload event of the window. This has generally worked okay, apart from when I try adding any JavaScript that is outside of a function.

    For example, I was trying to add a conditional addLoadEvent function call based on the existence of certain elements, which I was detecting by document.getElementById. It went something like this...

    Code:
    if (document.getElementById("box")){
    
    addLoadEvent(changeBox);
    }
    This, as you can probably gather, did not work!

    Could anyone tell me why? Why do functions that would work perfectly normally within the "root" of a standard script (i.e. not in a function), no longer work within the "root" when using the addLoadEvent method of DOM scripting? I only appear to be able to use the basic 'window' functions (including, thankfully, alert!) I get the impression that I am somehow "outside the DOM" but I can't quite get my head around it!

    Anyone care to help me out?

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The DOM does not allow access to elements of the page that have not loaded yet. Any code to access the DOM that is in the head of the page but not in a function called after the page finishes loading will not work because the objects in the page that it refers to don't exist yet.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Member
    Join Date
    Jul 2005
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Of course...!

    Of course...!

    I feel such a fool. This is pretty obvious stuff, and fundamental to the whole idea of DOM Scripting. D'oh!

    I think what was throwing me was being able to access window properties (e.g. location.href) from outside of a function, which obviously do not require the page to have loaded first.

    Oh well, never mind - lesson learnt! Thankyou for your assistance!

  4. #4
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by The Skipper
    I feel such a fool.
    Don't. I think everyone has had to have this pointed out to them at some point.


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
  •