3 ms·
Interesting. I had forgotten that synthesising events and dispatching them triggered the appropriate functionality. I knew it once, but for some reason I’d ende
by chrismorgan 7y ago
Interesting. I had forgotten that synthesising events and dispatching them triggered the appropriate functionality. I knew it once, but for some reason I’d ended up thinking that .click() was special.
Anyway, it’s not enough to simulate clicking on the anchor element, for two reasons:
• As hitherto stated, the anchor element is not there under the mouse; so any other interactions definitely won’t work properly. e.g. hover should show the link destination in the status bar, and right click should open a context menu with items like “open link”, “open link in new tab”, “open link in private window”, “send link”, &c. I don’t believe that either of these can be triggered programmatically.
• It doesn’t have the desired effect across all browsers. In fact, I find it surprising that a synthesised click event would have modifiers work in any browser, and I’m inlined to call it a bug. The synthesised event doesn’t have the modifiers like ctrlKey set on it, so if Chrome is trying to be helpful by guessing that you probably want the modifiers to apply even to the faked event (which is a dubious assumption) then it puts it at odds with event handlers, which will see events without ctrlKey et al. And if you try to set ctrlKey in Firefox (haven’t tried in any other browser) then it ignores the event (which I find a tad surprising, but I’m guessing it’s a form of browser hijack protection), so there doesn’t seem to be any way to make this simulation approach work in Firefox.
There’s a lot you can do in browsers, but there’s also rather a lot of user agent functionality that can’t be faked, like <a href> and scrolling. And so for those sorts of things, I say: use the native functionality, and accept no substitutes.
I am heartened to see how carefully you’ve been thinking all these things through. Nice job!