SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to call a function for every onmouseover a link?

    Hi I am doing a GreaseMonkey script and I need to call a function "myFunction" everytime a user hover over any link on the page.

    Please help.
    Last edited by vizualbod; Dec 12, 2007 at 09:30.

  2. #2
    SitePoint Zealot tconley79's Avatar
    Join Date
    Feb 2004
    Location
    Miami
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    knee jerk reply...untested
    Code:
    links = body.getElementsByTagName('a')
    
    for(var i = 0; i<links.length; i++){
      var link=links[i];
      link.onmouseover=function(){
         //your function operations
      }
    }

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    All events bubble up, so you can have just the one capture, instead of hundreds.

    Code JavaScript:
    var document.body.onmouseover = function(evt) {
      var e = (window.event) ? window.event : evt;
      // your function
    }

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd be careful with this one if you need to maintain any existing functionality that people have added to links.

    sticking functions in an array which you later iterate over, executing as you go is one way to do this
    Mashups, DOM Scripting and all the things we call Web2.0 blogger
    http://www.heavyweightgeek.org/
    http://twitter.com/heavyweightgeek

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @pmw57
    not all events bubble to the top, though I do like your thinking. A technique I've used many a time.
    Mashups, DOM Scripting and all the things we call Web2.0 blogger
    http://www.heavyweightgeek.org/
    http://twitter.com/heavyweightgeek


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
  •