简体   繁体   English

Chrome-单击mailto:链接会关闭websocket连接

[英]Chrome - clicking mailto: links closes websocket connection

I'm using the latest stable Chrome, version 41 . 我正在使用最新的稳定版Chrome,版本41。 I have an open websocket connection on the page and a link to an email address (mailto:***). 我在页面上有一个开放的websocket连接,并有一个指向电子邮件地址(mailto:***)的链接。 When the user clicks on the email address the websocket connection is closed. 当用户单击电子邮件地址时,Websocket连接将关闭。 Firefox doesn't have this issue. Firefox没有这个问题。 Do you know how to fix this? 你知道如何解决这个问题吗?

Thank you 谢谢

For whatever reason, when you click a mailto: link on the same page, Chrome and Firefox will kill any open websockets (and possibly any active XHR connections). 无论出于何种原因,当您单击同一页面上的mailto:链接时,Chrome和Firefox都会杀死所有打开的websocket(以及可能的任何活动XHR连接)。

So to get around this, you can hijack the click and call window.open(hrefMailtoTarget) . 因此,为了解决这个问题,您可以劫持点击并调用window.open(hrefMailtoTarget) This will leave your connections open and start the user's email client, but you'll notice that you now have a new blank browser page, so you might try window.open(hrefMailtoTarget).close() , which will immediately close that newly opened browser page... but it won't open the email client. 这将使您的连接保持打开状态并启动用户的电子邮件客户端,但是您会注意到您现在拥有一个新的空白浏览器页面,因此可以尝试使用window.open(hrefMailtoTarget).close() ,它会立即关闭新打开的页面。浏览器页面...,但不会打开电子邮件客户端。

So to globally fix all email links with jQuery: 因此,要使用jQuery在全球范围内修复所有电子邮件链接,请执行以下操作:

$(document).on('click', 'a[href^="mailto:"]', function (e) {
    e.preventDefault();
    var emailWindow = window.open($(e.currentTarget).attr('href'));

    setTimeout(function () {
        emailWindow.close();
    }, 500); // Is half a second long enough?
             // I don't know.
             // I'd set it as long as you can stand.

    return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM