【问题标题】:mailto: new window but 'To' field address is mailto:user@test.commailto:新窗口,但“收件人”字段地址为 mailto:user@test.com
【发布时间】:2015-02-23 12:33:20
【问题描述】:

我有一个 javascript 在新窗口中打开邮件客户端以发送电子邮件:

<a onClick="javascript:window.open('mailto:user@test.com', 'Mail');event.preventDefault()" href="mailto:user@test.com">Send a e-mail</a>

但问题是,当 Yahoo / Gmail 客户端撰写页面时,“To”字段默认设置为“ma​​ilto:user@test.com”这实际上应该是“user@test.com

我们怎样才能做到这一点 - 我只需要在新页面上打开电子邮件客户端

HTML 代码 - 请注意它会打开一个 iframe 窗口作为目标:

<!DOCTYPE html>
<html dir="ltr">
<head>
<style>
span {
    float: left;
    width: 0.7em;
    font-size: 400%;
    font-family: algerian, courier;
    line-height: 80%;
}
p {
    letter-spacing: 2px;
    line-height: 200%;
}
</style>
</head>
<body>
<hr align="centre" width="50%"/> 
<p align="justify">
<img src="./images/dev.jpg" align="left" width="300px height="400px" hspace="20"/>
<strong><span>A</strong></span>t a time ... </p>
<h1>Contact:</h1>
<p align="justify">
<a href="http://www.google.com" target="_blank">Google</a><br/>
Professor<br/>
<a href="mailto:user@test.com" target="_blank">Send An e-mail</a>
<a onClick="window.open('mailto:user@test.com', 'Mail');event.preventDefault();" href="mailto:user@test.com">Send a e-mail</a>
</p>
</body>
</html>

【问题讨论】:

  • OT:onclick 属性中不需要 javascript:。仅当您将 JS 放入通常包含 URL 的属性中时才需要这样做。
  • 你为什么使用javascript?只需使用 href 属性中的 mailto 即可获得相同的行为。
  • 它不会打开新页面 - 它使用默认页面使用户从应用程序中移出
  • 所以只需添加 target="_blank" 属性。查看@dr_dev 答案。
  • Firebug 显示我按顺序打开以下 URL:compose.mail.yahoo.com/?To=mailto%3Auser%40test.com 然后in-mg61.mail.yahoo.com/neo/…

标签: javascript jquery


【解决方案1】:

我使用的是最新的 chrome 版本(39.*),“发送电子邮件”和“发送电子邮件”链接都会为我打开一个新的标签页..

由于我已将浏览器配置为默认打开 mailto 链接以使用 Gmail 打开,因此它会打开 gmail 页面并打开一个撰写窗口,其中 to 字段设置为 user@test.com 而不是 mailto:user@test.com

Fiddle

【讨论】:

    【解决方案2】:

    这是因为 google 和 yahoo 期望 url 的查询字符串是 "?to=" 而不是 "?mailto=" 详情请见this question

    【讨论】:

    • 谢谢 - 我在想问题出在我的代码上。然后我知道我们没有办法纠正这个问题,因为我们不知道哪个客户会选择任何方式
    【解决方案3】:

    请试试这个,在下面的例子中不需要使用javascript。

    <a href="mailto:user@test.com?Subject=Mail" target="_top">Send Mail</a>
    

    MORE DETAILS

    【讨论】:

    • 它没有在新窗口中打开
    • @Prakash:Keenan Lidral-Porter 是正确的.. 雅虎和谷歌接受“to”而不是“mailto”:)
    【解决方案4】:

    您已将 window.open 中的 url 指定为“mailto:user@test.com”,这就是它在新窗口中打开该 url 的原因。如果您的要求是使用提到的地址(即“user@test.com”)打开邮件客户端,您可以使用以下行简单地完成。

    <a href="mailto:user@test.com?Subject=Mail" target="_blank">Send an e-mail</a>
    

    【讨论】:

    • 好吧,我在 iframe 中有窗口,当我将您的代码用于测试电子邮件客户端时,不会在新页面中打开
    • 将目标值改为“_top”,看看是否有效。
    猜你喜欢
    • 2016-05-28
    • 2010-12-18
    • 2021-10-08
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 2023-02-21
    • 1970-01-01
    相关资源
    最近更新 更多