mail作为默认邮件处理程序的用户的新选项卡中打开mailto链

mail作为默认邮件处理程序的用户的新选项卡中打开mailto链

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

问题描述

在网页上,mailto链接打开默认的电子邮件客户端。既然Chrome能够将Gmail设置为默认的电子邮件客户端,一些用户可以在同一个窗口中打开链接,从而使他们远离他们点击链接的页面(他们不喜欢)。



我已经尝试将链接添加到目标_blank,这对Gmail用户非常有用,但会导致Outlook用户发疯,因为每次点击邮件链接时都会打开一个新的空白标签。 / p>

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

解决方案

好的,我可以在Mac上使用Chrome。你的旅费可能会改变。此外,这是相当hacky国际海事组织,所以它可能不值得。老实说,这应该存在于Chrome中的一个设置,并应该将行为委托给网站。例如。 Chrome应该有一个选项:[x]总是在单独的标签中打开mailto链接



这就是说,你是这么做的。



首先构建您的链接,如下所示:

 < a href =#data-mailto [email protected]>邮寄某人< / a> 

然后设置一个点击处理程序。

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

有一个可选的选项 code> window.open ,你可以调整。事实上,我几乎会推荐它,以查看是否可以使生成的窗口尽可能不可觉察。



(MDN文档是详尽的,而w3schools文档更容易阅读)

接下来我们需要创建mailto.html页面。现在你可能需要在下面看到的超时时间玩。您甚至可以将其设置为像500毫秒这样非常短的内容。

 < html> 
< script>
函数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。应用程序设置为我的默认电子邮件阅读器:



当我点击链接时,它会打开一秒钟的窗口,然后编写一条空白消息。在浏览器中,它会返回原始页面。



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



当我点击链接时,它会为Gmail打开一个新标签页,并将其前一页安全地放在自己的标签中。 >注意:将Gmail设置为电子邮件处理程序后,在操作系统端(至少在Mac上),将Chrome设置为系统的电子邮件处理程序。因此,即使您将Gmail作为Chrome内部的电子邮件处理程序关闭,它仍然设置在操作系统级别。所以重置,我去了邮件>首选项>常规。并将默认邮件阅读器设置回邮件。


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)

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?

解决方案

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="[email protected]">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;
});

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 (the MDN doc is exhaustive, while the w3schools doc is almost easier to read)

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>

Results

Mail.app set as my default email reader:

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 set as mail reader under Settings > Advanced > Privacy > Handlers:

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

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链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 22:50