40 users online (0 members and 40 guests)  


  Results 1 to 3 of 3

Related

  1. Opera problems    Forum: HTML Forum
    Replies: 2
  2. Mozilla Firefox released!    Forum: General Discussion
    Replies: 2
  3. Anyone heard about an Opera Browser?    Forum: HTML Forum
    Replies: 3
  4. Opera 7 is spyware? Or is this old news?    Forum: Webmaster Ethics
    Replies: 4
  5. Replies: 1
  1. #1
    mawooshen's Avatar
    New User

    Status
    Offline
    Join Date
    Jun 2004
    Posts
    2

    getAttribute() & Netscape/Mozilla/Opera etc...

    Hi there,

    Can anybody tell me how to use getAttribute() with Netscape 7.1, Opera 7 and the latest Mozilla?

    The code below works in IE 6 but not in any of the above browsers and it seems to be something to do with getAttribute. If it is a compatibility or syntax issue, is there any resource or reference that I can refer to when writing code?

    <html>
    <head>
    <script type="text/javascript">
    function whtColor()
    {
    theId=document.getElementById('divtag').style;
    alert(theId.getAttribute('color'));
    }
    </script>
    </head>

    <body>
    <a href="javascript:whtColor()">What color is it?</a>
    <div id="divtag" style="position:absolute; top:400px; left:400px; color:#000000;" >What color am I?</div>
    </body>
    </html>

    Thanks,

  2. #2
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732
    Hi there mawooshen,

    and a warm welcome to these forums

    The problem here is that 'color' is not an attribute of a <div>
    whereas 'style' is. 'I.E.' is rendering incorrectly. To get round this
    I have used an 'if' statement so that Opera and Mozilla browsers
    will display the full style of your <div>
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function whtColor() {
    if(navigator.userAgent=='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; CDSource=BP1b.00)') {
    
    theId=document.getElementById('divtag').style;
         alert(theId.getAttribute('color'));
     }
    else {
    theId=document.getElementById('divtag');
         alert(theId.getAttribute('style'));
     }
    }
    </script>
    </head>
    
    <body>
    <a href="javascript:whtColor()">What color is it?</a>
    <div id="divtag" style="position:absolute; top:400px; left:400px; color:#000000;" >What color am I?</div>
    </body>
    </html>
    There is probably a shorter 'if' statement than the one I used but as
    it worked and served it's purpose I did not bother to find it


    coothead

  3. #3
    mawooshen's Avatar
    New User

    Status
    Offline
    Join Date
    Jun 2004
    Posts
    2
    Thanks a lot coothead, spent a lot of time scratching my head over that one!
    Javascript seems so simple, but I keep on getting stumped by the cross-browser incompatibility thing...

    I'll be back with more questions before long!