jQuery-contextMenu icon indicating copy to clipboard operation
jQuery-contextMenu copied to clipboard

Anchor tag clicks aren't followed (html5)

Open mustanggb opened this issue 7 years ago • 10 comments

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;

mustanggb avatar Mar 24 '17 14:03 mustanggb

Hmm, seems reasonable. Not sure if it would propangate though.

Any news if this fixes it for you?

bbrala avatar Apr 03 '17 15:04 bbrala

Yes it works as noted above, jQuery events are triggered, and the native event (href location change) is triggered.

mustanggb avatar Apr 03 '17 15:04 mustanggb

@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.

mustanggb avatar May 03 '17 12:05 mustanggb

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.

mustanggb avatar May 05 '17 15:05 mustanggb

Ah good catchy, I skimmed the linter instead of fully checking it.

bbrala avatar May 05 '17 16:05 bbrala

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.

screen shot 2017-05-05 at 3 51 34 pm

cgatesman avatar May 05 '17 20:05 cgatesman

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?

bbrala avatar May 06 '17 13:05 bbrala

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().

cgatesman avatar May 08 '17 16:05 cgatesman

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 avatar May 08 '17 16:05 cgatesman

@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.

mustanggb avatar Jul 06 '17 15:07 mustanggb