使用 mailto (html) 打开带有 chrome 的选项卡

Using mailto (html) opens a tab with chrome

我正在开发一个网站,使其无需在脚本中输入电子邮件即可使用 mailto。 这是函数

<script type= "text/javascript"> 
    function createemail(name,subject) 
      {
      var email = name + '@email.com';
      var mailto_link = 'mailto:' + email + '?subject=' + subject;
      win = window.open (mailto_link, 'emailWindow');
      //if (window && window.open && !window.closed) window.close()----DOES NOT CLOSE WINDOW };
  </script>

然后我使用 onclick 命令引用该函数

   <tr>
  <td align="center" valign="middle">
    <font size="+1"><a onclick = "createemail('TEST','Subject Text')"; style= "cursor:pointer; cursor:hand; color:#0000ee"><u>Sales</u></a></font>
  </td>
</tr>

文本更改为看起来像超链接,但它只是引用函数

我不完全确定你想做什么,但如果我是对的,你正试图在电子邮件 window 打开后关闭 window。

你不能那样做。

Javascript只能关闭一个被它打开的window。有关详细信息,请参阅 this question

P.S. 真的,你为什么要用font?

好吧,如果您希望该锚标记的行为打开您的电子邮件客户端(Outlook 或其他),那么您必须使用 mailto_link 更改锚标记的 innerHTML。您必须为该锚标记设置一个 id,然后使用 document.getElementById('anchorTagId') 您可以稍后设置它。

function createemail(name,subject) 
{
var email = name + '@email.com';
var mailto_link = 'mailto:' + email + '?subject=' + subject;
win = window.open (mailto_link, 'emailWindow');
//if (window && window.open && !window.closed) window.close()----DOES NOT CLOSE WINDOW
var _theEmail = document.getElementById('theEmail');
_theEmail.innerText = mailto_link;
};


<table>
    <tr>
        <td align="center" valign="middle">
          <font size="+1"><a id="theEmail" onclick = "createemail('TEST','Subject Text')"; style= "cursor:pointer; cursor:hand; color:#0000ee"><u>Sales</u></a></font>
        </td>
      </tr>
</table>

我将函数更改为

  <script type= "text/javascript"> 
function createemail(name,subject) 
  {
  var email = name + '@bldgcs.com';
  location.href = 'mailto:' + email +'?subject=' + subject;
  };

location.href 似乎解决了问题