JavaScript Menubox Visibility Error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jmensch@shaw.ca

    #1

    JavaScript Menubox Visibility Error

    I'm reasonably new to JavaScript and DHTML,
    so I'm sorry if this is an obvious mistake.

    The following snippet of code is intended to
    make the menu -- the absolute-positioned <div>
    section -- disappear when one mouses over the
    graphic, but instead it generates a "document.
    menu_admin has no properties" error in Firefox
    and does nothing in IE. Can someone tell me
    what it is that I'm doing wrong here?

    Ironically, when you replace "menu_admin "
    with "omv" in the function, it works fine to
    make the image disappear, but no such luck
    with the <div>-box.

    -- Julian Mensch

    ----------------------------------------------

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>Untitl ed Page</title>

    <style type="text/css">
    <!--
    .menu1
    {
    position: absolute;
    left: 100px;
    top: 100px;
    background-color: #000077;
    color: #FFFFFF;
    z-index: 2;
    visibility:visi ble;
    }
    -->
    </style>


    </head>
    <body>

    <div id="menu_admin " class="menu1">
    <b>Admin Pages</b>
    <br>Add New Book
    <br>List Popular
    <br>Edit User Accounts
    <br>Backup Database
    </div>

    <img id="omv" src="tab_admin. gif" onMouseOver="on MouseOver1()">

    <script language="javas cript">
    function onMouseOver1()
    {
    document.menu_a dmin.style.visi bility = "hidden";
    }
    </script>
    </body>
    </html>

  • Lasse Reichstein Nielsen

    #2
    Re: JavaScript Menubox Visibility Error

    jmensch@shaw.ca writes:
    [color=blue]
    > The following snippet of code is intended to
    > make the menu -- the absolute-positioned <div>
    > section -- disappear when one mouses over the
    > graphic, but instead it generates a "document.
    > menu_admin has no properties" error in Firefox[/color]

    You are expecting the document element to contain a property
    with the same name as the id attribute of your div element.
    It doesn't. The correct way to access an element with an id
    is to use
    document.getEle mentById("menu_ admin")
    and not just "document.menu_ admin" or even "menu_admin ".

    [color=blue]
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    > "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">[/color]

    You are claiming to write XHTML ...
    [color=blue]
    > <html xmlns="http://www.w3.org/1999/xhtml" >
    > <head>
    > <title>Untitl ed Page</title>
    >
    > <style type="text/css">
    > <!--[/color]

    .... and in XHTML, a comment like this would make the entire
    content of the style element disappear. "Luckily", the browser
    doesn't treat your page as XHTML.

    I recommend just using HTML for now.
    [color=blue]
    > <div id="menu_admin " class="menu1">[/color]
    ....[color=blue]
    > </div>
    >
    > <img id="omv" src="tab_admin. gif" onMouseOver="on MouseOver1()">[/color]

    The "alt" attribute is required on img elements.

    In XHTML, attribute names are case sensitive, so it should be
    "onmouseove r" in all small letters.
    [color=blue]
    > <script language="javas cript">[/color]

    Should be
    <script type="text/javascript">
    The "type" attribute is required and always sufficient.
    [color=blue]
    > function onMouseOver1()
    > {
    > document.menu_a dmin.style.visi bility = "hidden";[/color]

    This should then be:
    document.getEle mentById("menu_ admin").style.v isibility = "hidden";

    Good luck
    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • RobG

      #3
      Re: JavaScript Menubox Visibility Error

      jmensch@shaw.ca wrote:[color=blue]
      > I'm reasonably new to JavaScript and DHTML,
      > so I'm sorry if this is an obvious mistake.
      >
      > The following snippet of code is intended to
      > make the menu -- the absolute-positioned <div>
      > section -- disappear when one mouses over the
      > graphic, but instead it generates a "document.
      > menu_admin has no properties" error in Firefox
      > and does nothing in IE.[/color]

      It generates an error in IE too, you may not have IE setup to report
      scripting errors. Read the group FAQ:

      <URL:http://www.jibbering.c om/faq/>
      [color=blue]
      > Can someone tell me
      > what it is that I'm doing wrong here?
      >
      > Ironically, when you replace "menu_admin "
      > with "omv" in the function, it works fine to[/color]

      Not 'ironically'. Browsers create an image collection that can be
      addressed the same way you address array elements, e.g.

      document.images[0]
      or
      document.images['omv']
      or
      document.images .omv

      or (less preferred)
      document.<image _name>
      or
      document.<image _id>

      You can do the same with forms, but not most other elements.
      [color=blue]
      > make the image disappear, but no such luck
      > with the <div>-box.
      >
      > -- Julian Mensch
      >
      > ----------------------------------------------
      >
      > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
      > "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
      > <html xmlns="http://www.w3.org/1999/xhtml" >[/color]

      Your doctype indicates XHTML, but your code is HTML 4. Which do you
      really want? Probably HTML 4 is the best option unless you have a
      good reasons for XHTML.
      [color=blue]
      > <head>
      > <title>Untitl ed Page</title>
      >
      > <style type="text/css">
      > <!--[/color]

      There is no need to hide style declarations, it is likely harmful if
      you really do want XHTML.
      [color=blue]
      > .menu1
      > {
      > position: absolute;
      > left: 100px;
      > top: 100px;
      > background-color: #000077;
      > color: #FFFFFF;
      > z-index: 2;
      > visibility:visi ble;
      > }
      > -->[/color]

      And here.
      [color=blue]
      > </style>
      >
      >
      > </head>
      > <body>
      >
      > <div id="menu_admin " class="menu1">
      > <b>Admin Pages</b>
      > <br>Add New Book
      > <br>List Popular
      > <br>Edit User Accounts
      > <br>Backup Database
      > </div>
      >
      > <img id="omv" src="tab_admin. gif" onMouseOver="on MouseOver1()">
      >
      > <script language="javas cript">[/color]

      The language attribute is depreciated, type is required:

      <script type="text/javascript">
      [color=blue]
      > function onMouseOver1()
      > {
      > document.menu_a dmin.style.visi bility = "hidden";[/color]

      You can't refer to a document element this way, use something like:

      if (document.getEl ementById) {
      var x = document.getEle mentById('menu_ admin');
      } else if (document.all) {
      var x = document.all['menu_admin'];
      } else { return; }

      if ( x && x.style ) {
      x.style.visibil ity = 'hidden';
      }
      [color=blue]
      > }
      > </script>
      > </body>
      > </html>
      >[/color]


      --
      Rob

      Comment

      • jmensch@shaw.ca

        #4
        Re: JavaScript Menubox Visibility Error

        Thank both of you graciously for the reply.
        The top two lines that claim to write XHTML
        are in fact auto-generated by Microsoft Web
        Developer 2005 for ASP.NET, so I'm not sure
        I should change them.

        I will familiarize myself more completely with
        the differences between HTML 4 and XHTML,
        however. This warning probably saved me a
        good deal of anguish in the future, so now I
        owe the two of you twice-over. :)

        I will read the FAQ in more detail, too.

        -- Julian Mensch

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: JavaScript Menubox Visibility Error

          Lasse Reichstein Nielsen wrote:
          [color=blue]
          > jmensch@shaw.ca writes:[color=green]
          >> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
          >> "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">[/color]
          >
          > You are claiming to write XHTML ...
          >[color=green]
          >> <html xmlns="http://www.w3.org/1999/xhtml" >
          >> <head>
          >> <title>Untitl ed Page</title>
          >>
          >> <style type="text/css">
          >> <!--[/color]
          >
          > ... and in XHTML, a comment like this would make the entire
          > content of the style element disappear.[/color]

          s/would/could/

          An XML parser is not obliged to remove comments, but is allowed to do so:

          <http://www.w3.org/TR/2004/REC-xml-20040204/#sec-comments>


          PointedEars

          Comment

          Working...