Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Why is fooDiv null?

    Hi there, I'm having a problem with a null reference that I can't seem to figure out. I'd like to dynamically work with the fooDiv object referenced from the document.writes as you can see from the following snippet.
    Code:
    <html>
    <head>
        <script language="javascript" type="text/javascript">
    <!--
        document.write('<div id="foo"><\/div>');
        document.write('<script language="javascript" type="text/javascript">');
        document.write('var fooDiv = document.getElementById("foo");'); 
        //do sweet stuff with fooDiv
        document.write('<\/script>');
    </head>
    The problem is that fooDiv is null when i try to access it.
    Can someone help me out? I'm loosing my hair on this one :<

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Wait for the page to be loaded before trying to access and manipulate it:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    window.onload = function() {
    	var fooDiv = document.getElementById("foo");
    	alert(fooDiv);
    }
    </script>
    </head>
    
    <body>
    <script type="text/javascript">
    document.write('<div id="foo">&nbsp;</div>');
    </script>
    </body>
    </html>
    Hope this helps.

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your response martin, but that's not really what I'm lookin for unfortunately.
    What I was really trying to do is interact with the dynamically created DIV while the page is still rendering, without having to wait for the onload event to fire.

    I noticed that if I use the following code the DIV is accessible:
    Code:
    <html>
    <head>
        <script language="javascript">
    <!--
        document.write('<div id="foo"><\/div>');
    
    -->
       </script>
    //some other tag
        <script language="javascript">
    <!--
        document.write('<script language="javascript" type="text/javascript">');
        document.write('var fooDiv = document.getElementById("foo");'); 
        //do sweet stuff with fooDiv
        document.write('<\/script>');
    -->
       </script>
    </head>
    Notice that between the two <script> tags there is some other tag...for example a css block or a meta tag. Strangely this works even if the page hasn't finished loading. Probably the scripting engine flushes the code to the browser's rendering engine at the end of the first <script> tag, so that when the second block of JS code runs the browser already has a reference to the objects.

    The problem is that I'm loading the script from a src attribute from an external .js file, and I have to keep everything contained within the single .js file.


  •  

    Posting Permissions

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