SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    Bored One boredboi's Avatar
    Join Date
    Jul 2000
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I have a absolutely positioned <div id="div1"> nested in a relatively positioned <div>,

    document.layers['div1'].visibility = 'hidden';
    or
    document.div1.visibility = 'hidden';

    gives an error of:
    document.div1 has no properties.

    Does anyone know what's the problem?
    Thanks.

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Netscape considers the contents of each DIV/LAYER as a completely new document; thus, to access 'div1' you need to assign your outer div an ID (say 'wrapper') and then use document.wrapper.document.div1.

    Here's an example:

    Code:
    <html>
    <head>
    <title> type_Document_Title_here </title>
    </head>
    <body>
    <div id="wrapper" style="position:relative">
        <div id="div1" style="position:absolute;top:100;left:100;width:50;height:50">
          <table bgcolor="red" border=0 width=50 height=50>
            <tr>
              <td valign=middle align=center>
                <font color="white">Hi!</font>
              </td>
            </tr>
          </table>
        </div>
    </div>
    <script language="JavaScript">
    <!--
    function blink(setVisibility) {
    	if (document.layers) { // NS4
    	  document.wrapper.document.div1.visibility = (setVisibility ? "visible" : "hidden");
    	} else if (document.all) { // MSIE
    	  document.all.div1.style.visibility = (setVisibility ? "visible" : "hidden");
    	}
    	setTimeout("blink("+!setVisibility+")",1000);
    }
    
    setTimeout("blink(false)",1000);
    
    //-->
    </script>
    </body>
    </html>
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  3. #3
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey guys,

    Kevin's exactly right. In NS 4, to access layers and stuff, you can't leave out anything. This is just like accessing an image in a layer. In IE 4+ and NS 6+, you can just go:

    document.images["imageName"]

    But in NS 4, you'd have to go:

    document.layers["layerID"].document.images["imageName"]

    A lot more, huh?

    Now, Kevin's code will work, but it won't work in NS 6. for NS 6 to access a layer, it uses the W3C reccomended way of document.getElementById (IE 5 can also use this way). Anyway, you can just change Kevin's function to this:

    Code:
    function blink(setVisibility) {
    	if(document.getElementById){ //IE 5 and NS 6
    	  document.getElementById("div1").style.visibility = (setVisibility ? "visible" : "hidden");
    	} else if (document.layers) { // NS4
    	  document.wrapper.document.div1.visibility = (setVisibility ? "visible" : "hidden");
    	} else if (document.all) { // MSIE
    	  document.all.div1.style.visibility = (setVisibility ? "visible" : "hidden");
    	}
    	setTimeout("blink("+!setVisibility+")",1000);
    }

    aDog


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
  •