jQuery-contextMenu
jQuery-contextMenu copied to clipboard
Anchor tag clicks aren't followed (html5)
As per: https://makandracards.com/makandra/17267-trigger-a-link-s-click-action-with-javascript
$node.click();
only triggers jQuery registered click events, it doesn't trigger the native element click, and hence the href link isn't followed.
The best option seems to be $node[0].click();
which fires both the jQuery events and the native event.
I'm currently testing this, although the same issue might be pertinent to button
also:
// http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#using-the-a-element-to-define-a-command
case 'a':
+ item = {
+ name: $node.text(),
+ disabled: !!$node.attr('disabled'),
+ callback: (function () {
+ return function () {
+ $node[0].click();
+ };
+ })()
+ };
+ break;
+
// http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#using-the-button-element-to-define-a-command
case 'button':
item = {
name: $node.text(),
disabled: !!$node.attr('disabled'),
callback: (function () {
return function () {
$node.click();
};
})()
};
break;
Hmm, seems reasonable. Not sure if it would propangate though.
Any news if this fixes it for you?
Yes it works as noted above, jQuery events are triggered, and the native event (href location change) is triggered.
@bbrala Anything else you need from me? If you're after a pull request then some insight into exactly which direction you'd like to go would be appreciated.
Awesome thanks, to keep consistency with the rest of your codebase you might want to include the terminating semi-colon, not a big deal but thought I'd mentioned it as usually they aren't included as an oversight rather than on purpose.
Ah good catchy, I skimmed the linter instead of fully checking it.
I feel there is still a problem, here. I think a lot of times people may use an anchor as an item would be to have target="_blank"
to open in another tab. However, I believe this fix doing a click()
would make the browser complain that it appears to be a popup, and block it.
As far as i know jQuery's .click()
would really be handled the same by the browser? So i don't see how this change would affect the users differently than they are now?
It's apparently, not. My screenshot of that warning was from me simulating a click on an <a href="..." target="_blank">
with jQuery's click()
.
From what I've read, popup blockers will typically only allow if used during the processing of a user event (like a click).
However, I believe the contextMenu is taking any direct clicks and triggering a new event for processing. Since this is happening, the processing code isn't delayed and not under the direct processing of the the user's click. Therefore, it will be treated as popup which should be blocked.
The problem is similar to what is described, here:
https://stackoverflow.com/questions/9514698/bypass-popup-blocker-on-window-open-when-jquery-event-preventdefault-is-set
@cgatesman Not sure if this should be a seperate issue tbh, however I tested with Firefox 54.0.1 and didn't have any problems with _blank
.