使用 JQuery/JavaScript 调用/单击 mailto 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3868315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invoke / click a mailto link with JQuery / JavaScript
提问by Justin
I'd like to invoke a mailtolink 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?
我怎样才能做到这一点?
回答by Nick Craver
You can use window.location.href
here, like this:
你可以window.location.href
在这里使用,像这样:
window.location.href = "mailto:[email protected]";
回答by franzo
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>
回答by Toskan
the working answer for me, tested in chrome, IE and firefox together with outlook was this
对我来说,在 chrome、IE 和 firefox 以及 Outlook 中测试的工作答案是这个
window.location.href = 'mailto:[email protected]?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
是应该使用的空格符号,但它对我也适用于普通空格
回答by s34n
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. This works on current Firefox/Chrome and IE (also IE will display a short confirm dialog).
我发现,您可以简单地将带有 mailto 链接的 iframe 插入到 dom 中。这适用于当前的 Firefox/Chrome 和 IE(IE 也会显示一个简短的确认对话框)。
Using jQuery, I got this:
使用 jQuery,我得到了这个:
var initMailtoButton = function()
{
var iframe = $('<iframe id="mailtoFrame" src="mailto:[email protected]" 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);
});
}
}