最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Defining JavaScript Event Object - Stack Overflow

matteradmin4PV0评论

Why am I getting an error for this piece of code?:

function catchevent() 
{
    eventSrcID=(event.srcElement)?event.srcElement.id:'undefined';

    eventtype=event.type;

    status=eventSrcID+' has received a '+eventtype+' event.';
}

Firefox says that event is not defined. Actually, this is copied from here and it clearly says that it is for IE5. I use Firefox 3.6.13 on Ubuntu.

My Question is not why it doesn't work on my browser. Instead, is there a way to define event object, as suggested by the link, that would work for my browser?

UPDATE

Why isn't this working

<html>
<head>
<script>
function catchevent( e ) {
      // reference the proper event object
    var e = e || window.event;

      // reference the proper target
    var srcElem = e.target || e.srcElement; //line no 9

      // get the id of the target
    var eventSrcID = srcElem.id;

    alert(eventSrcID);
}
</script>
</head>
<body>
    <a id="link1" href="#" onclick="catchevent()">link1</a>
    <a id="link2" href="#" onclick="catchevent()">link2</a>
    <a id="link3" href="#" onclick="catchevent()">link3</a>

</body>
</html>

For this i am still getting error on line no 9

e is undefined
[Break On This Error] var srcElem = e.target || e.srcElement; 

Now how do I need to pass event object on catchevent() or is there some mistake? please suggest.

Why am I getting an error for this piece of code?:

function catchevent() 
{
    eventSrcID=(event.srcElement)?event.srcElement.id:'undefined';

    eventtype=event.type;

    status=eventSrcID+' has received a '+eventtype+' event.';
}

Firefox says that event is not defined. Actually, this is copied from here and it clearly says that it is for IE5. I use Firefox 3.6.13 on Ubuntu.

My Question is not why it doesn't work on my browser. Instead, is there a way to define event object, as suggested by the link, that would work for my browser?

UPDATE

Why isn't this working

<html>
<head>
<script>
function catchevent( e ) {
      // reference the proper event object
    var e = e || window.event;

      // reference the proper target
    var srcElem = e.target || e.srcElement; //line no 9

      // get the id of the target
    var eventSrcID = srcElem.id;

    alert(eventSrcID);
}
</script>
</head>
<body>
    <a id="link1" href="#" onclick="catchevent()">link1</a>
    <a id="link2" href="#" onclick="catchevent()">link2</a>
    <a id="link3" href="#" onclick="catchevent()">link3</a>

</body>
</html>

For this i am still getting error on line no 9

e is undefined
[Break On This Error] var srcElem = e.target || e.srcElement; 

Now how do I need to pass event object on catchevent() or is there some mistake? please suggest.

Share Improve this question edited Feb 20, 2011 at 4:45 S L asked Feb 20, 2011 at 4:23 S LS L 14.3k18 gold badges79 silver badges119 bronze badges 1
  • 2 I might suggest to use a guide less focused on an ancient browser with historically awful standards support. – sarnold Commented Feb 20, 2011 at 4:30
Add a ment  | 

4 Answers 4

Reset to default 9

In Firefox and other W3C pliant browsers, the handler function will receive an event object as an argument to the function.

IE uses a global event object.

Here's a cross browser solution for your code.

function catchevent( e ) {
      // reference the proper event object
    var e = e || window.event;

      // reference the proper target
    var srcElem = e.target || e.srcElement;

      // get the id of the target
    var eventSrcID = srcElem.id;

      // get the event type
    var eventtype = e.type;

      // create the status string
    var status = eventSrcID + ' has received a ' + eventtype + ' event.';
}

You'll notice I used the var keyword when creating variables. You should do this, otherwise you're creating global variables.

Also note that the keyword this will be a reference to the element to which the handler was assigned.

you forgot to pass event in parameters.

function catchevent(event) 
{
    eventSrcID=(event.srcElement)?event.srcElement.id:'undefined';

    eventtype=event.type;

    status=eventSrcID+' has received a '+eventtype+' event.';
}

In Firefox, the first param passed to a handler is the event. So a cross browser piece of code would check for a param and use it if present. If not present, it would default to window.event, the IE way.

The event object is a global variable in internet explorer. (Actually a property of the global window variable)

Firefox is simply a different design, and event is not a global variable in firefox. Instead, it is passed into the function (so you would need to change catchEvent() to accept a parameter:

function catchEvent(event){

Take a look at this artice because that is not the only problem you'll face with the event model:

http://www.quirksmode/js/introevents.htmlQuirksmode article

Post a comment

comment list (0)

  1. No comments so far