5

I am trying to get which event is occured ::

function getEvent(){
    alert(window.event);
}

I am getting below event object. I want it in string to compare. if event onclick event then I want to do the action. how can I get it in string? or how can I convert event object to string?

[object MouseEvent]
1
  • Why do you need this? Can you give an example? Commented Oct 17, 2012 at 10:39

5 Answers 5

7

Use the type property. window.event.type

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for you. I am getting "TypeError: window.event is undefined" when I am try to use alert(window.event.type ) in Mozilla browser, Its working well with Google Chrome.
The error is pretty self explanatory, window.event is not defined. Firefox handles events slightly differently from chrome. You should change your code to function getEvent(e){alert(e.type);}
After using your code function getEvent(e){alert(e.type);} I am getting "ReferenceError: e is not defined". call of getEvent function is getEvent(event);
3

You'd probably want:

function getEvent(){
    alert(window.event.type);
}

https://developer.mozilla.org/en-US/docs/DOM/event.type

Comments

1

you can easily get it by type like this

function getEvent(){
    alert(window.event.type);
}

More info here

http://www.quirksmode.org/js/events_access.html

http://www.quirksmode.org/js/events_properties.html

Comments

1

use the type property to read out the type:

function getEvent(){
    alert(window.event.type);
}

Comments

1

Use the event.type property, for example

<div id="a" onclick="alert(event.type)">Click This</div>

<div id="b">And This</div>​

Javascript:

var func = $('#a').attr('onclick');
$('#b').mouseover(function(e) {
    func(e)
});

DEMO: http://jsfiddle.net/4tLms/

1 Comment

I've just tested it and working fine. Do you have javascript turned on?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.