Command-click doesn't open a new tab, but middle-click does

前端 未结 3 1997
眼角桃花
眼角桃花 2021-02-03 23:42

On my website, which is a one-page JS site using Sammy.js and jQuery, when I middle-click a link with a mouse, the link opens in a new tab. But when I command-click on a Mac, it

3条回答
  •  野的像风
    2021-02-04 00:13

    Here's the relevant code from sammy.js:

    // bind to link clicks that have routes
    $('a').live('click.history-' + this.app.eventNamespace(), function(e) {
        if (e.isDefaultPrevented()) {
            return;
        }
        var full_path = lp.fullPath(this);
        if (this.hostname == window.location.hostname && app.lookupRoute('get', full_path)) {
            e.preventDefault();
            proxy.setLocation(full_path);
            return false;
        }
    });
    

    Summary: If someone clicks on a link, override the standard link behavior with sammy.js's behavior, which is to change the current page to show the content of the target page with no actual page load. According to dakdad's link, a command-click (unlike a middle-click) is caught by the click event, and can be overridden.

    For a workaround, you could remove sammy.js's event handler (using $('a').die('click.history-' + _sammy_event_namespace_);) and replace it with a modified version that checks for command-clicks and avoids overriding them.

提交回复
热议问题