jQuery 在新标签页/窗口中打开 href mailto 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8549816/
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
Open the href mailto link in new tab / window
提问by shennyL
I have an image which when click, I want to link to a mailto:
我有一张图片,当点击时,我想链接到一个mailto:
<a id="mailto" href="mailto:[email protected]" target="_newtab" >
<img src="@Url.Content("~/Content/HomePage/email.png")" alt="email" /></a>
However, currently once its clicked, it will launch the email option to choose a mailto application, and once i choose, the mailto link is open in the current tab. This will cause user to leave the application.
但是,目前一旦点击它,它就会启动电子邮件选项来选择一个 mailto 应用程序,一旦我选择,mailto 链接就会在当前选项卡中打开。这将导致用户离开应用程序。
So, I want the page to sent email (by gmail, yahoo, etc ) is either open in new tab or in a window. Any idea how to do this? I tried both target="_newtab" and target="_blank" but both didn't work.
所以,我希望发送电子邮件的页面(通过 gmail、yahoo 等)要么在新标签页中打开,要么在窗口中打开。知道如何做到这一点吗?我尝试了 target="_newtab" 和 target="_blank" 但都没有奏效。
Any help will be much appreciated.. Thanks...
任何帮助将不胜感激..谢谢...
(jQuery method is also acceptable if there is no other way, thanks)
(如果没有其他方法也可以接受jQuery方法,谢谢)
采纳答案by Scott
mailto calls the users default email client. It does not open a window or tab in any instance. If you want to use a window or tab you need to configure a form and allow the form to open in your window/tab. Of course, you'll have to configure the form to send mail with whatever method is available on your server.
mailto 调用用户默认的电子邮件客户端。它在任何情况下都不会打开窗口或选项卡。如果您想使用窗口或选项卡,您需要配置一个表单并允许该表单在您的窗口/选项卡中打开。当然,您必须配置表单以使用服务器上可用的任何方法发送邮件。
回答by RedScourge
this information is outdated, now it is possible to do so i believe, since gmail and others now work via browser links. there is however the problem that you would only want it to open in a new tab if NOT opening in a system mail client, and open in a new tab if it is a webmail client, otherwise for example Outlook users see a blank tab appear, which is disorienting, especially since they are Outlook users.
此信息已过时,我相信现在可以这样做,因为 gmail 和其他人现在通过浏览器链接工作。然而,存在的问题是,如果不在系统邮件客户端中打开,您只希望它在新选项卡中打开,如果它是网络邮件客户端,则在新选项卡中打开,否则例如 Outlook 用户会看到一个空白选项卡出现,这令人迷惑,尤其是因为他们是 Outlook 用户。
回答by Marcos Eusebi
This answer is based on this answer Open the href mailto link in new tab / window.
此答案基于此答案在新选项卡/窗口中打开 href mailto 链接。
Right now, new browsers support someweb mail interfaces (Like Gmail, Yahoo Mail, AoL, etc.).
目前,新的浏览器支持一些网络邮件接口(如 Gmail、Yahoo Mail、AoL 等)。
So we can simply open a new window (Support older browser, new browsers just will open a new tab) and add a fallback (In case of non-javascript user) using preventDefault and default link redirection.
所以我们可以简单地打开一个新窗口(支持旧浏览器,新浏览器只会打开一个新选项卡)并使用 preventDefault 和默认链接重定向添加回退(如果是非 JavaScript 用户)。
http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-cancelation
http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-cancelation
https://developer.mozilla.org/es/docs/DOM/event.preventDefault
https://developer.mozilla.org/es/docs/DOM/event.preventDefault
https://developer.mozilla.org/en-US/docs/Web/API/Window.open
https://developer.mozilla.org/en-US/docs/Web/API/Window.open
Like so:
像这样:
<a onClick="javascript:window.open('mailto:[email protected]', 'mail');event.preventDefault()" href="mailto:[email protected]">Send a e-mail</a>
<a onClick="javascript:window.open('mailto:[email protected]', 'mail');event.preventDefault()" href="mailto:[email protected]">Send a e-mail</a>
Credit to https://stackoverflow.com/a/9880404/1107020
归功于https://stackoverflow.com/a/9880404/1107020
Guess that's all.
猜猜这就是全部。
Greetings, Marcos.
问候,马科斯。
回答by Costa
You don't need Javascript/Jquery for this. A standard link works (except Firefox v30+ due to a bug, see below).
为此,您不需要 Javascript/Jquery。标准链接有效(由于存在错误,Firefox v30+ 除外,请参见下文)。
<a href="mailto:[email protected]" target="_blank">
As of Firefox 30, does not work in Firefox due to a bug. It opens in the same tab AND replaces history so hitting back will not bring you back to the page where the mailto: link was.
从 Firefox 30 开始,由于bug无法在 Firefox 中工作。它在同一个选项卡中打开并替换历史记录,因此回击不会将您带回 mailto: 链接所在的页面。
回答by Sampson Crowley
I know this is an old question, but this thread had the best set of answers if found. I modified Marcos's Answer above to also close the blank tab that is created if the client has an external mail handler
我知道这是一个老问题,但是如果找到这个线程,它会提供最好的答案。我修改了上面的 Marcos 的答案,以关闭如果客户端有外部邮件处理程序时创建的空白选项卡
JS (w\ jQuery for event handlers)
JS (w\ jQuery for event handlers)
$(document).on('click', 'a[href^=mailto]', function(e) {
var checkClose, checkLoaded, event, href, i, len, loadEvents, results, t, wndw;
e.preventDefault();
href = this.href;
wndw = window.open(href, 'mail');
checkClose = function() {
console.log('checkClose');
try {
wndw.location.href;
return wndw.close();
} catch (error) {
return console.log('webmail');
}
};
t = setTimeout(checkClose, 5000);
try {
checkLoaded = function() {
console.log('loaded');
clearTimeout(t);
return t = setTimeout(checkClose, 2000);
};
wndw.onload = checkLoaded;
loadEvents = ["DomContentLoaded", "load", "beforeunload", "unload"];
results = [];
for (i = 0, len = loadEvents.length; i < len; i++) {
event = loadEvents[i];
results.push(wndw.addEventListener(event, checkLoaded));
}
return results;
} catch (error) {
return checkLoaded();
}
});
回答by Botyk
Variant 1 (JavaScript):
变体 1(JavaScript):
<script>
// Open mailto links in a new tab
function mailto(email, subject, body) {
var url;
url = 'mailto:' + email;
url += '?subject=' + subject;
url += '&body=' + body;
window.open(url);
}
</script>
<a href="#" onclick="mailto('[email protected]', 'Subject', 'Body');event.preventDefault()">[email protected]</a>
Variant 2 (JavaScript):
变体 2 (JavaScript):
<script>
// Open mailto links in a new tab
function mailto(th) {
var url = th.getAttribute('href');
window.open(url);
}
</script>
<a href="mailto:[email protected]?subject=Subject&body=Body" onclick="mailto(this);event.preventDefault()">[email protected]</a>
Variant 3 (jQuery):
变体 3 (jQuery):
<script>
// Open mailto links in a new tab
$('#mailto').click(function (e) {
e.preventDefault();
var url = $(this).attr('href');
window.open(url);
});
</script>
<a href="mailto:[email protected]?subject=Subject&body=Body" id="mailto">[email protected]</a>
Variant 4 (jQuery):
变体 4 (jQuery):
<script>
// Open mailto links in a new tab
$("a[href^='mailto:']").click(function(e) {
e.preventDefault();
var href = $(this).attr('href');
var target = $(this).attr('target');
window.open(href, target ? target : '_self');
});
</script>
<a href="mailto:[email protected]?subject=Subject&body=Body" target="_blank">[email protected]</a>
HTML target Attribute: https://www.w3schools.com/tags/att_a_target.asp
HTML 目标属性:https: //www.w3schools.com/tags/att_a_target.asp
回答by mtsdev
Have you tried 'middle-click' ( "Open in new tab" ) ? It works for me
您是否尝试过“中键单击”(“在新标签页中打开”)?这个对我有用
(http://forums.mozillazine.org/viewtopic.php?f=7&t=1842595)
( http://forums.mozillazine.org/viewtopic.php?f=7&t=1842595)
although it seems particularly strange to ask user to Middle click
虽然让用户中间点击似乎特别奇怪
Anyway I've found a pseudo solution that seems to work in FF 25/ Chrome 35
无论如何,我找到了一个似乎适用于 FF 25/Chrome 35 的伪解决方案
1.- Set up your link something like this:
1.- 像这样设置你的链接:
<a href="javascript:void()"
class="mailToLink"
data-mail="[email protected]">[email protected] </a>
2.- Using javascript ( with jquery in the example) setup an onlclick event like:
2.- 使用 javascript(在示例中使用 jquery)设置一个 onlclick 事件,如:
$('.mailToLink').on('click', function(){
mailto=$(this).data('mail');
w=window.open('','_blank','',true);
w.location.href='mailto:'+mailto;
w.focus();
});
This opens a blank new window/tab and later changes its location, so the mail protocol handler is unable toto act until the new window is already opened
这会打开一个空白的新窗口/选项卡,然后更改其位置,因此邮件协议处理程序在新窗口打开之前无法执行操作
Not tested with Local mail client ( Outlook et al.)
未使用本地邮件客户端(Outlook 等)进行测试
回答by Rollins
There is a cheap html-hack to this problem.....
这个问题有一个廉价的 html-hack ......
The link on one page...
一页上的链接...
<a href="/mailto.html" target="_blank">Mail</a>
On mailto.html....
在mailto.html上....
<meta HTTP-EQUIV="REFRESH" content="0; url=mailto:[email protected]">
If nothing pops up click.....<a href="mailto:[email protected]">Mail!</a>
_blank opens a new tab/window and the metatag does the rest. link as fallback offcourse.
_blank 打开一个新选项卡/窗口,其余的由元标记完成。链接作为后备offcourse。