Web Analytics Made Easy -
StatCounter Problem with Onmouseover in Window.Onload - CodingForum

Announcement

Collapse
No announcement yet.

Problem with Onmouseover in Window.Onload

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with Onmouseover in Window.Onload

    Hi,
    I am trying to use img.onmouseover inside window.onload. For examples, I want some images on the webpage to do something when visitor moves their mouse over it. But I don't want to put onmouseover inside the tag in html and I decided to do it in window.onload for design reason, and I do img.onmouseover=myfunction() in window.onload. I get the img from document.getElementsByTagName('img'), then get the correct img by traversing the tree.

    I move my mouse to the image, it does what I want. But the problem is that once I move my mouse out of the image and move back in it, it doesn't do it anymore.

    I tried to do img.onmouseout= function(){img.onmouseover=myfunction();}; but it still doesn't do it. Do you know how to fix it?

    Many thanks.
    Last edited by dealmaker; Aug 3, 2005, 09:48 PM.

  • #2
    You will still need to assign the event to the img via your javascript so that it will trigger each time. Use an id for the img and then have the onload perform the assignment to the onmouseover event for that tag/id.

    BaldEagle

    Comment


    • #3
      I already get the correct img by using document.getElementsByTagName('img') then traverse the tree. So, I think I already assigned the event to this img. I can't use tag or id for design reason.

      Don't forget that it works for the first mouseover, it 's just that it doesn't work after I mouseout and then mouseover again.


      Originally posted by BaldEagle
      You will still need to assign the event to the img via your javascript so that it will trigger each time. Use an id for the img and then have the onload perform the assignment to the onmouseover event for that tag/id.

      BaldEagle

      Comment


      • #4
        Sorry Dealmaker, I didn't read carefully enough. Maybe post some code to se what you are doing and why it doesn't want to work.

        BaldEagle

        Comment


        • #5
          The problem could be in the onmouseover function itself and not in window.onload.

          BTW, there is a similar thread here. Double-posting is not allowed so you should ask the mod to delete it.
          Glenn
          vBulletin Mods That Rock!

          Comment


          • #6
            You are assigning the return value of the function to the onmouseover method, rather than the function itself.

            Code:
            // the result value of myfunction is assigned. If it returns a function this should be OK
            img.onmouseover = myfunction();
            
            // now you are assigning the function itself
            img.onmouseover = myfunction;
            
            // now you are assigning a function which calls myfunction with the event object and 'some variable' as arguments on mouse over
            img.onmouseover = function(ev) { myfunction(ev, 'some variable'); }
            dumpfi
            "Failure is not an option. It comes bundled with the software."
            ....../)/)..(\__/).(\(\................../)_/)......
            .....(-.-).(='.'=).(-.-)................(o.O)...../<)
            ....(.).(.)("}_("}(.)(.)...............(.)_(.))¯/.
            ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
            Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

            Comment


            • #7
              dumpfi, the discussion flows on the other thread now.
              dealmaker, as a respect to other members reading this thread, it's your responsibility to inform people here that the discussion is now on-going on your other thread.
              Glenn
              vBulletin Mods That Rock!

              Comment

              Working...
              X