[英]Invoke / click a mailto link with JQuery / JavaScript
I'd like to invoke a mailto link from JavaScript - that is I'd like a method that allows me to open the email client on the users PC, exactly as if they had clicked on a normal mailto link.我想从 JavaScript 调用一个mailto链接 - 那就是我想要一种方法,它允许我在用户 PC 上打开电子邮件客户端,就像他们点击了一个普通的 mailto 链接一样。
How can I do this?我怎样才能做到这一点?
你可以在这里使用window.location.href
,如下所示:
window.location.href = "mailto:address@dmail.com";
You can avoid the blank page issue discussed above by instead using .click() with a link on the page: 您可以通过使用.click()和页面上的链接来避免上面讨论的空白页问题:
document.getElementById('mymailto').click();
...
<a href="mailto:...." id="mymailto" style="display:none"></a>
the working answer for me, tested in chrome, IE and firefox together with outlook was this 我的工作答案,在chrome,IE和firefox以及outlook中进行测试就是这个
window.location.href = 'mailto:address@dmail.com?subject=Hello there&body=This is the body';
%0d%0a
is the new line symbol of the email body in a mailto link %0d%0a
是mailto链接中电子邮件正文的新行符号
%20
is the space symbol that should be used, but it worked for me as well with normal space %20
是应该使用的空格符号,但它对我和普通空间都有效
Better to use更好用
window.open('mailto:address@mail.com?subject=sub&body=this is body')
If we use window.location.href
chrome browser is having error in network tab with Provisional headers are shown Upgrade-Insecure-Requests: 1如果我们使用window.location.href
chrome 浏览器在网络选项卡中出现错误,并显示临时标头 Upgrade-Insecure-Requests: 1
This is an old question, but I combined several Stack Overflows to come up with this function:这是一个老问题,但我结合了几个 Stack Overflows 来提出这个函数:
//this.MailTo is an array of Email addresses
//this.MailSubject is a free text (unsafe) Subject text input
//this.MailBody is a free text (unsafe) Body input (textarea)
//SpawnMail will URL encode /n, ", and ', append an anchor element with the mailto, and click it to spawn the mail in the users default mail program
SpawnMail: function(){
$("#MyMailTo").remove();
var MailList="";
for(i in this.MailTo)
MailList+=this.MailTo[i]+";";
var NewSubject=this.MailSubject.replace(/\n/g, "%0d%0a");
NewSubject=NewSubject.replace(/"/g, "%22");
NewSubject=NewSubject.replace(/'/g, "%27");
var NewBody=this.MailBody.replace(/\n/g, "%0d%0a");
NewBody=NewBody.replace(/"/g, "%22");
NewBody=NewBody.replace(/'/g, "%27");
$("#mainNavBar").after("<a id='MyMailTo' style='display:none;' href='mailto:"+MailList+"?subject="+NewSubject+"&body="+NewBody+"'> </a>");
document.getElementById('MyMailTo').click();
}
The cool part about this (and how I plan to use it) is I can put this in a loop and split out individual messages to everyone in the array or keep everyone together (which is what this function currently does).关于这个(以及我计划如何使用它)很酷的部分是我可以把它放在一个循环中,并将单独的消息拆分给数组中的每个人,或者让每个人都在一起(这是这个函数目前所做的)。 Anyway thanks for the tip @Toskan无论如何感谢您的提示@Toskan
FOLLOW-UP - Please note the new HTML5 standard does not allow looping mailto (or other pop-up related js) without a "required user gesture".后续- 请注意,新的 HTML5 标准不允许在没有“必需的用户手势”的情况下循环 mailto(或其他与弹出窗口相关的 js)。 Cool article here: https://github.com/WICG/interventions/issues/12 So you can't use this to mass generate individual e-mails, but it does work well with sending to many in it's current design.这里很酷的文章: https : //github.com/WICG/interventions/issues/12所以你不能用它来批量生成单独的电子邮件,但它在当前设计中可以很好地发送给许多人。
Actually, there is a possibillity to avoid the empty page. 实际上,有可能避免空白页面。
I found out, you can simply insert an iframe with the mailto link into the dom. 我发现,你可以简单地将一个iframe与mailto链接插入到dom中。 This works on current Firefox/Chrome and IE (also IE will display a short confirm dialog). 这适用于当前的Firefox / Chrome和IE(IE也将显示一个简短的确认对话框)。
Using jQuery, I got this: 使用jQuery,我得到了这个:
var initMailtoButton = function()
{
var iframe = $('<iframe id="mailtoFrame" src="mailto:name@domain.com" width="1" height="1" border="0" frameborder="0"></iframe>');
var button = $('#mailtoMessageSend');
if (button.length > 0) {
button.click(function(){
// create the iframe
$('body').append(iframe);
//remove the iframe, we don't need it any more
window.setTimeout(function(){
iframe.remove();
}, 500);
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.