www.webdeveloper.com
Results 1 to 7 of 7

Thread: Don't execute if property not supported?

  1. #1
    Join Date
    Nov 2004
    Posts
    17

    Don't execute if property not supported?

    I've written a little function for the mouseover so that a button can change colour in IE. Only thing is, Mozilla doesn't recognise the style property and gives a javascript error. How do I get my code to not execute if the property is not supported? The if statement that I'm using isn't working.


    Code:
    <input name="target1" class="myClass" type="submit" onMouseOver="doMouseOver('target1');"  value="hello"/>
    Code:
    function doMouseOver(srcName)
    {
      srcObj=document.getElementById(srcName);
      if (srcObj.style) {
        srcObj.style.backgroundColor = "#ffcc00";
      }
    }

  2. #2
    Join Date
    Jan 2005
    Posts
    21
    This article may help. Most modern browsers (including FireFox and Mozilla) support adding ":hover" to the style of any tag. In this case your style code would look something like this:
    Code:
    <style>
    .myClass:hover {
       background-color: #ffcc00;
    }
    </style>
    IE will ignore it so you'll have to rewrite the javascript to be IE only.

    Incidentally, I don't think your problem is with Style recognition; I think it has more to do with your assignment of srcObj through getElementById. If you take advantage of JavaScript's "this" object your mouseover problem disappears (well, in FireFox anyway).
    Code:
    <script language=javascript>
    function doMouseOver(srcObj)
    {
        srcObj.style.backgroundColor = "#ffcc00";
    }
    </script>
    ...
    <input name="target1" class="myClass" type="submit" onMouseOver="doMouseOver(this);" value="hello"/>

  3. #3
    Join Date
    Nov 2003
    Location
    Aachen, Germany
    Posts
    4,090
    Hi!

    I was just about to post something similar, using 'this'. I deleted that part after previewing my post and seeing yours, ccamarat.

    The error in Pingu's code is simply based on the fact, that the button has no 'id', only a 'name'.

    Cheers - Pit

  4. #4
    Join Date
    Jan 2005
    Posts
    21
    "Must remember to pay attention to detail" - d'Oh!

  5. #5
    Join Date
    Nov 2003
    Location
    Aachen, Germany
    Posts
    4,090
    Originally posted by ccamarat
    "Must remember to pay attention to detail" - d'Oh!
    Yeees!

  6. #6
    Join Date
    Nov 2004
    Posts
    17
    Thank you, that worked!

    Where can I find a good guide to JavaScript so I can work these things out for myself?

  7. #7
    Join Date
    Jan 2005
    Posts
    21
    I mostly use Google because most of my questions have already been answered. Wrox publishing has books on most everything; they have helped me immensely (probably because most of the Beginning series assumes no knowledge of anything). They’re the thick red books in the programming section of your local Border’s. Another trick I like doing is "View Source". Break it - then figure out why it doesn't work (my dad used to hate it when I did that to his toys! )
    Last edited by ccamarat; 01-20-2005 at 05:45 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles