SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why doesn't 'event ' work in firefox browser?

    I am writing a simple javascript code to capture which element raised an event. When I run this code it works okay in IE. But Firefox says 'event' not defined.

    Also I am using Microsoft Visual studio 2008 to create this file. And for somereason, there is no context sensitive help when I am using event object. Why is that?


    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Javascript_CreateaDynamicTable.aspx.cs" Inherits="FHLBSF.QRMDMS.WebUI.Javascript_CreateaDynamicTable" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <img alt="" src="media/fhlbsflogo.gif" />
      
      <p style="position:absolute;top:10;left:10;color:black" onmouseout="OnMouseOut()" onmouseover="OnMouseoverEventHandler()"> Demo of "OnMouseOver" handler</p>
      
     <table border="1">
      <thead>
        <tr>
          <th>Month</th>
          <th>Savings</th>
        </tr>
      </thead>
      <tfoot id="tfootelement" onmousemove="WhichElementIsThis();">
        <tr>
          <td id="tdSum" onclick="WhichElementIsThis();" >Sum</td>
          <td>$180</td>
        </tr>
      </tfoot>
      <tbody>
        <tr >
          <td>January</td>
          <td>$100</td>
        </tr>
        <tr>
          <td>February</td>
          <td>$80</td>
        </tr>
      </tbody>
    </table>
    
     </div>
        
        </form>
    </body>
    
    <script type="text/javascript" language="javascript">
    
                function WhichElementIsThis() {
    
        
    
                var varElementId = event.srcElement.id;
    
                alert(varElementId);
                
                
             
            }
    
       </script>
        
    
    </html>

  2. #2
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    There are two ways to access the "event" object, based on the browser (some browsers might support both, I can't remember). One way is the global "event" object, which IE6-8 supports and which you are using.

    The other way to do it is assume that the event object has been passed to the event handler, like this:

    Code JavaScript:
    function WhichElementIsThis(event) {
        // ...
    }

    Certain browsers (Firefox, Chrome, IE9) will automatically pass the event object to your function. So for cross-browser compatability, you could do something like this:

    Code JavaScript:
    function WhichElementIsThis(event) {
        event = event || window.event;
        // ...
    }

    Off Topic:


    Another cross-browser issue you'll run into is that not all browsers support the 'srcElement' property; some use 'target' instead, so you'll want to do this, too:

    Code JavaScript:
    function WhichElementIsThis(event) {
        event = event || window.event;
        var elem = event.target || event.srcElement;
        // ...
    }
    I'm the web overlord for Graphic Business Systems


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
  •