如何为将 gmail 作为默认邮件处理程序的用户在新选项卡中打开 mailto 链接? [英] How to open mailto links in new tab for users that have gmail as the default mail handler?

查看:47
本文介绍了如何为将 gmail 作为默认邮件处理程序的用户在新选项卡中打开 mailto 链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网页上的 mailto 链接打开默认的电子邮件客户端.现在 Chrome 提供了将 Gmail 设置为默认电子邮件客户端的功能,一些用户会在同一窗口中打开链接,从而使他们离开点击链接的页面(他们不喜欢)

On a web page mailto links open the default e-mail client. Now that Chrome offers the ability to set Gmail as the default e-mail client, some users have the links open in the same window thus taking them away from the page they clicked the link (which they do not like)

我尝试将目标 _blank 添加到链接,这对 gmail 用户非常有用,但会让 Outlook 用户发疯,因为每次点击 mailto 链接时都会打开一个新的空白选项卡.

I have tried adding target _blank to the links, which works great for gmail users, but will drive Outlook users mad, because a new blank tab will open every time they click a mailto link.

我有办法检测默认电子邮件处理程序并为这两种类型的用户提供良好的体验吗?

I there a way to detect the default e-mail handler and offer a good experience for both types of users?

推荐答案

好的,所以我能够在 Mac 上的 Chrome 中使用它.你的旅费可能会改变.此外,这对 IMO 来说是很老套的,所以它可能不值得.老实说,这应该作为 Chrome 中的设置存在,并且应该将行为委托给网站.例如.Chrome 应该有一个选项:[x] 始终在单独的选项卡中打开 mailto 链接"

Okay, so I was able to get this working in Chrome on Mac. Your mileage may vary. Also, this is pretty hacky IMO, so it may not be worth it. Honestly this should exist as a setting within Chrome, and the behavior should be delegated to the website. E.g. Chrome should have an option: "[x] Always open mailto links in separate tab"

话虽如此,这就是你的做法.

That being said, here's how you do it.

首先像这样构建你的链接:

First construct your links like so:

<a href="#" data-mailto="somebody@email.com">Mail Somebody</a>

然后为这些设置一个点击处理程序.

Then set a click handler for those.

$('a[data-mailto]').click(function(){
  var link = 'mailto.html#mailto:' + $(this).data('mailto');
  window.open(link, 'Mailer');
  return false;
});

window.open 有一个可选的 options 参数,您可以对其进行调整.事实上,我几乎会推荐它,看看你是否可以让生成的窗口尽可能不引人注目.https://developer.mozilla.org/en/DOM/window.open

There is an optional options argument to window.open that you can tweak. In fact I would almost recommend it, to see if you can get the generated window to be as unnoticable as possible. https://developer.mozilla.org/en/DOM/window.open

http://www.w3schools.com/jsref/met_win_open.asp(MDN 文档详尽无遗,而 w3schools 文档几乎更容易阅读)

http://www.w3schools.com/jsref/met_win_open.asp (the MDN doc is exhaustive, while the w3schools doc is almost easier to read)

接下来我们需要创建mailto.html 页面.现在您可能需要使用下面看到的超时.您甚至可以将其设置为非常短的值,例如 500 毫秒.

Next we need to create the mailto.html page. Now you may need to play around with the timeout you see below. You could probably even set this to something really short like 500ms.

<html>
<script>
function checkMailto(hash){
    hash = hash.split('mailto:');
    if(hash.length > 1){
        return hash[1];
    } else {
        return false;
    }
}

var mailto = checkMailto(location.hash);

if(mailto){
    location.href = 'mailto:'+mailto;
    setTimeout(function(){
      window.close();
    }, 1000);
}
</script>
</html>

结果

Mail.app 设置为我的默认电子邮件阅读器:

当我单击该链接时,它会在瞬间打开一个窗口,然后撰写一条空白消息.在浏览器中它会返回到原始页面.

When I click the link, it opens a window for a split second, then composes a blank message. In the browser it goes back to the original page.

在设置">高级">隐私">处理程序"下将 Gmail 设置为邮件阅读器:

当我点击该链接时,它会打开一个新的 Gmail 标签页,上一页安全地位于它自己的标签页中.

When I click the link, it opens a new tab to Gmail, with the previous page safely in it's own tab.

注意:一旦您将 Gmail 设置为您的电子邮件处理程序,在操作系统端(至少在 mac 上),Chrome 将被设置为系统的电子邮件处理程序.因此,即使您在 Chrome 中关闭 Gmail 作为电子邮件处理程序,它仍然是在操作系统级别设置的.所以要重置它,我去了邮件>首选项>常规.并将默认邮件阅读器设置回邮件.

Note: Once you set Gmail as your email handler, on the OS side (at least on mac), Chrome is set as the system's email handler. So even if you turn off Gmail as the email handler inside Chrome, it is still set on the OS level. So to reset that, I went to Mail > Prefs > General. And set default mail reader back to Mail.

这篇关于如何为将 gmail 作为默认邮件处理程序的用户在新选项卡中打开 mailto 链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆