Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Aug 2004
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts

    accessing to event in a function

    i have some html:

    <div id='mydiv' onmouseover='sth('ff',this);'>blah</div>


    how can i access the event object in a function, without changing parameters?

    in IE, i can use simply event, but in others(mozilla)?


    and is any other way to see the object, who fired an event, without giving 'this' in a parameter of sth()?

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,495
    Thanks
    3
    Thanked 500 Times in 487 Posts
    PHP Code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd"
    >

    <
    html>

    <
    head>
      <
    title></title>
    <
    script language="JavaScript" type="text/javascript">
    <!--
    function 
    Event(evt){
      var 
    e=evt||window.event;
      if(!
    e) return;
      if(
    e.type){ this.type e.type;   alert(e.type); }
      if(
    e.target) {this.target e.target;  alert(e.target.tagName);}
      else if(
    e.srcElement){ this.target e.srcElement;  alert(e.srcElement.tagName); }
    }

    //-->
    </script>
    </head>

    <body>

    <span onclick="Event(event);" >document.Show.Show1.value=vic;</span>
    </body>

    </html> 

  • #3
    Regular Coder
    Join Date
    Aug 2004
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no, no, no.

    i DONT wanna give event through parameters of function.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Quote Originally Posted by looka
    i DONT wanna give event through parameters of function.
    You have to, to make it work with Moz browsers.

    But if you assign the event programmatically, you don't have to pass the event object as it is passed internally.

    Code:
    <div id="mydiv">blah</div>
    <script type="text/javascript">
    function myFunc(evt){
      alert(evt);
      if (!evt) evt = event;
      alert(evt);
    }
    
    document.getElementById("mydiv").onmouseover = myFunc;
    </script>
    But if you hardcode the handler, you have to pass the event.

    <div id="mydiv" onmouseover="myFunc(event)">blah</div>

  • #5
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    function sth(blabla) {
    var event = window.event || arguments.callee.caller.arguments[0];

    // do stuff with event now
    }

    You don't need to pass in the event object explicitly through an argument. Mozilla and the like generate an internal function to represent the event handler, which is passed the event object and calls the actual javascript in the attribute value. So arguments.callee.caller accesses this internal function, and arguments[0] grabs the event object.

  • #6
    Regular Coder
    Join Date
    Aug 2004
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i just love posts like that, thanks


  •  

    Posting Permissions

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