Web Analytics Made Easy -
StatCounter How To Set Onmouseover in Javascript Without Running The Function? - CodingForum

Announcement

Collapse
No announcement yet.

How To Set Onmouseover in Javascript Without Running The Function?

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

  • How To Set Onmouseover in Javascript Without Running The Function?

    Hi,
    Instead of setting the image's onmouseover in img tag in html, I need to set it in Javascript .js file instead.

    My code (inside .js file):

    img.onmouseover = myfunction(mypara);

    I find that if I put this in .js file, it will run myfunction() no matter what, even without moving the mouse over.

    How do I set onmouseover inside .js file without actually running the function?

    Many thanks.
    Last edited by dealmaker; Aug 4, 2005, 12:49 AM.

  • #2
    I just realized something, I researched online and found that all code I saw so far are in format of:

    img.onmouseover = myfunction;

    There is no "(mypara)". I tested it in my own code, and I found that it finally doesn't run myfunction. But all it doesn't pass any parameter I want into myfunction().

    So, how do I pass parameters into functions like this if I do it this way?

    Comment


    • #3
      Code:
      img.onmouseover = function() {
      myfunction(mypara);
      }
      or
      Code:
      img.onmouseover = new Function('myfunction(mypara);')
      - John

      Comment


      • #4
        Double-post!

        You should keep the discussions into one to avoid confusion and waste of time and resources.
        Glenn
        vBulletin Mods That Rock!

        Comment


        • #5
          If I do that, mypara will be void by the time I move the mouse over.

          How do I keep the value of mypara as I set the onmouseover?

          I try not to use global variable.

          Well, if I have to use global variable, then I have to find out which image the onmouseover belongs because there are several images on the webpage. I have no problem setting to specific img's onmouseover because I am using getElementsByTagName. I tried to use "this" inside myfunction() but "this" is pointing to "window", not the image. So how can I find a reference of the mouseover image when I am running the code inside myfunction()? Remember, I am putting all the code in window.onload, not in img tag.


          Originally posted by jscheuer1
          Code:
          img.onmouseover = function() {
          myfunction(mypara);
          }
          or
          Code:
          img.onmouseover = new Function('myfunction(mypara);')
          Last edited by dealmaker; Aug 4, 2005, 01:52 AM.

          Comment


          • #6
            ok, I solved the problem.

            This is weird, I guess I am too used to c and c++.

            Actually, in my actual code mypara is an array inside an array of array.

            If I pass it as img.onmouseover=function(){myfunction(array[i])}, it doesn't work.

            If I do :

            innerarray=array[i];
            img.onmouseover=function(){myfunction(innerarray);}, then it works.

            Comment


            • #7
              Code:
              img.onmouseover=new Function('myfunction('+array[i]+')')
              - John

              Comment


              • #8
                img.par=Ary[i]
                img.onmouseover=function(){ MyFunction(this.par); }
                Vic

                God Loves You and will never love you less.

                http://www.vicsjavascripts.org/Home.htm

                If my post has been useful please donate to http://www.operationsmile.org.uk/

                Comment


                • #9
                  Originally posted by vwphillips
                  img.par=Ary[i]
                  img.onmouseover=function(){ MyFunction(this.par); }
                  That's an IE memory leak.
                  Glenn
                  vBulletin Mods That Rock!

                  Comment


                  • #10
                    Are you sure that this works? I tried it but it didn't. Remember array[i] is an array since array is an array of array.

                    Originally posted by jscheuer1
                    Code:
                    img.onmouseover=new Function('myfunction('+array[i]+')')

                    Comment


                    • #11
                      Originally posted by dealmaker
                      Are you sure that this works? I tried it but it didn't. Remember array[i] is an array since array is an array of array.
                      It really depends upon the rest of the code and your objectives. If you want the literal value of array[i] at the moment:
                      Code:
                      img.onmouseover=new Function('myfunction('+array[i]+')')
                      is executed to become a part of the mouseover function, yes. Except, if it is a true literal (not a number or another variable) that will become a parameter of the onmouseover function, then it needs quotes:
                      Code:
                      img.onmouseover=new Function("myfunction('"+array[i]+"')")
                      if you want it to be whatever array[i] happens to be at the time the mouseover occurs:
                      Code:
                      img.onmouseover=new Function('myfunction(array[i])')
                      Additionally, unless alert(array[i]) returns array[i]'s value, all bets are off, and you need to define and/or utilize it more accurately. Again, very hard to say with such a small snippet and not knowing what you are trying to make happen.
                      - John

                      Comment


                      • #12
                        No, I am passing a 1 dimensional array as parameter. Following is the code:

                        2DimArray = new Array(new Array());
                        ...
                        2DimArray[i][j]=1;
                        ...
                        img.onmouseover=function() {myfunction(2DimArray[i]);} --> doesn't work, the parameter is void inside myfunction().

                        but:

                        2DimArray = new Array(new Array());
                        ...
                        2DimArray[i][j]=1;
                        ...
                        var 1DimArray=2DimArray[i];
                        img.onmouseover=function() {myfunction(1DimArray);} -> work

                        Originally posted by jscheuer1
                        It really depends upon the rest of the code and your objectives. If you want the literal value of array[i] at the moment:
                        Code:
                        img.onmouseover=new Function('myfunction('+array[i]+')')
                        is executed to become a part of the mouseover function, yes. Except, if it is a true literal (not a number or another variable) that will become a parameter of the onmouseover function, then it needs quotes:

                        Code:
                        img.onmouseover=new Function("myfunction('"+array[i]+"')")
                        if you want it to be whatever array[i] happens to be at the time the mouseover occurs:
                        Code:
                        img.onmouseover=new Function('myfunction(array[i])')
                        Additionally, unless alert(array[i]) returns array[i]'s value, all bets are off, and you need to define and/or utilize it more accurately. Again, very hard to say with such a small snippet and not knowing what you are trying to make happen.

                        Comment


                        • #13
                          Try this:
                          Code:
                          2DimArray[i][j]=1;
                          ...
                          setMouseover(img, i);
                          
                          ...
                          
                          function setMouseover(oImg, idx){
                            oImg.onmouseover = function() {myfunction(2DimArray[idx]);}
                          }
                          Glenn
                          vBulletin Mods That Rock!

                          Comment

                          Working...
                          X