使用 javascript 的 mailto
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10172499/
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
mailto using javascript
提问by Sathish
I want to open a new outlook mail template with the 'To address' whenever a user clicks an image. I have return my code in a html page(linked with the image), whenever it loads the javascript should open a new mail template. But the functionality is not working. Kindly let me know what is wrong in my code.
我想在用户单击图像时打开一个带有“收件人地址”的新 Outlook 邮件模板。我已经在 html 页面中返回了我的代码(与图像链接),每当它加载时,javascript 应该打开一个新的邮件模板。但该功能不起作用。请让我知道我的代码有什么问题。
body onLoad="redirect()"
script language="JavaScript"
function redirect()
var email = "[email protected]"
var mailto_link = 'mailto:' + email
window = window.open(mailto_link, 'emailWindow')
if (window && window.open && !window.closed)
window.close()
回答by StanE
No need for jQuery. And it isn't necessary to open a new window. Protocols which doesn't return HTTP data to the browser (mailto:
, irc://
, magnet:
, ftp://
(<- it depends how it is implemented, normally the browser has an FTP client built in)) can be queried in the same window without losing the current content. In your case:
不需要 jQuery。并且没有必要打开一个新窗口。不向浏览器返回 HTTP 数据的协议(mailto:
, irc://
, magnet:
, ftp://
(<- 这取决于它的实现方式,通常浏览器内置了 FTP 客户端))可以在同一窗口中查询而不会丢失当前内容。在你的情况下:
function redirect()
{
window.location.href = "mailto:[email protected]";
}
<body onload="javascript: redirect();">
Or just directly
或者直接
<body onload="javascript: window.location.href='mailto:[email protected]';">
回答by Ramesh
Please find the code in jsFiddle. It uses jQuery to modify the href of the link. You can use any other library in its place. It should work.
请在 jsFiddle 中找到代码。它使用 jQuery 来修改链接的 href。您可以在其位置使用任何其他库。它应该工作。
HTML
HTML
<a id="emailLnk" href="#">
<img src="http://ssl.gstatic.com/gb/images/j_e6a6aca6.png">
</a>
JS
JS
$(document).ready(function() {
$("#emailLnk").attr('href',"mailto:[email protected]");
});?
UPDATE
更新
Another code sample, if the id is known only during the click event
另一个代码示例,如果 id 仅在点击事件期间已知
$(document).ready(function() {
$("#emailLnk").click(function()
{
alert('h');
document.location.href = "mailto:[email protected]";
});
});?
回答by GuerraTron
With JavaScript you can create a link 'on the fly' using something like:
使用 JavaScript,您可以使用以下内容“即时”创建链接:
var mail = document.createElement("a");
mail.href = "mailto:[email protected]";
mail.click();
This is redirected by the browser to some mail client installed on the machine without losing the content of the current window ... and you would not need any API like 'jQuery'.
这由浏览器重定向到安装在机器上的某些邮件客户端,而不会丢失当前窗口的内容……而且您不需要任何像“jQuery”这样的 API。
回答by Allan
You can use the simple mailto
, see below for the simple markup.
您可以使用 simple mailto
,请参阅下面的简单标记。
<a href="mailto:[email protected]">Click here to mail</a>
Once clicked, it will open your Outlook or whatever email client you have set.
单击后,它将打开您的 Outlook 或您设置的任何电子邮件客户端。
回答by Proustibat
I've simply used this javascript code (using jquery but it's not strictly necessary) :
我只是使用了这个 javascript 代码(使用 jquery 但它不是绝对必要的):
$( "#button" ).on( "click", function(event) {
$(this).attr('href', 'mailto:[email protected]?subject=hello');
});
When users click on the link, we replace the href attribute of the clicked element.
当用户点击链接时,我们替换被点击元素的 href 属性。
Be careful don't prevent the default comportment (event.preventDefault), we must let do it because we have just replaced the href where to go
注意不要阻止默认的comportment(event.preventDefault),我们必须放手去做,因为我们刚刚把href替换到哪里去了
I think robots can't see it, the address is protected from spams.
我认为机器人看不到它,该地址受垃圾邮件保护。
回答by Camille Sauvage
I don't know if it helps, but using jQuery, to hide an email address, I did :
我不知道它是否有帮助,但是使用 jQuery 来隐藏电子邮件地址,我做到了:
$(function() {
// planque l'adresse mail
var mailSplitted
= ['mai', 'to:mye', 'mail@', 'addre', 'ss.fr'];
var link = mailSplitted.join('');
link = '<a href="' + link + '"</a>';
$('mytag').wrap(link);
});
I hope it helps.
我希望它有帮助。