0

My test case is @ Gmail, I am trying to simulate a click to open the first e-mail with JavaScript.

Actually I did a successful attempt which is done by firing a mousedown event.

But I wonder if there is a generic method to generate "organic" clicks instead of me wasting time on investigating attached events ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ozgur
  • 1,744
  • 4
  • 17
  • 24

1 Answers1

1

You can just trigger the usual suspects...but other than that, there's no good generic way to simulate a native click from a user (and an actual native click is impossible, for example making an <a> go to it href, without doing the redirect yourself).

Triggering the relevant events to a click for something like gmail would look like this:

$(selector).mousedown().mouseup().click();
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 1
    I don't agree with you about events can't make an anchor follow its href, because I did it with document.createEvent. However chaining seems the solution so far. Meh, it is time to hack webkit lol. – Ozgur Nov 14 '10 at 12:44
  • 1
    @Ozgur - No kidding, I'd love to see an example of this that works cross-browser. Unless something changed (AFAIK), IE still requires `createEventObject` instead, and even then you're not doing the *native* behavior, for example: Ctrl+Click opens in a new window, etc. That's why I emphasized *actual*, usually you're kind-of or partially simulating a native event, but not all the way there. – Nick Craver Nov 14 '10 at 20:04
  • You are right there is no cross-browser native click, but Firefox is the only outsider. Every other browser supports simulating a native `click` event and following `href`'s, obeying `target` attribute, etc. For reference: http://stackoverflow.com/questions/1421584/how-can-i-simulate-a-click-to-an-anchor-tag/1421968#1421968 – Crescent Fresh Nov 14 '10 at 20:43
  • @Crescent - That still doesn't seem to work for ctrl+click, shift+click, etc, doing whatever the user has setup for each (or defaults)...that's my point, it's not native, it's "kind of" native, missing pieces. – Nick Craver Nov 14 '10 at 20:46