Javascript 如何防止mailto事件在浏览器中打开新标签页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13457684/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:08:58  来源:igfitidea点击:

How to prevent mailto event from opening a new tab in browser

javascriptmailto

提问by Jeff Noel

I am using a mailto:filled in JavaScript to send information throughout my web application, but everytime a user presses the Sendbutton, it opens a new tab in the browser before opening the mailing application (Outlook, Gmail, etc).

我使用mailto:填充的 JavaScript 在整个 Web 应用程序中发送信息,但每次用户按下发送按钮时,它都会在打开邮件应用程序(Outlook、Gmail 等)之前在浏览器中打开一个新选项卡。

Is there any way to prevent the blank tab from opening?

有什么办法可以防止空白标签打开?



Edit: This issue is encountered in all following major browsers : Internet Explorer, Firefox and Google Chrome.

编辑:以下所有主要浏览器都会遇到此问题:Internet Explorer、Firefox 和 Google Chrome。

I am using window.open()to send emails, is there any known alternatives?

我正在使用window.open()发送电子邮件,是否有任何已知的替代方法?

Here is how I send the email:

这是我发送电子邮件的方式:

var mailto_link = 'mailto:'+email+'?subject='+subject+'&body='+body_message;
var win = window.open(mailto_link,'emailWindow');

I don't want to use window.location.hrefsince I am displaying a message after the user sent the email.

我不想使用,window.location.href因为我在用户发送电子邮件后显示一条消息。

采纳答案by AmShaegar

Thank you for the edit. There is indeed an alternative:

感谢您的编辑。确实有一个替代方案:

window.location.href = "mailto:[email protected]";
alert("Thank you!");

I don't want to use window.location.href since I am displaying a message after the user sent the email.

我不想使用 window.location.href 因为我在用户发送电子邮件后显示一条消息。

I did not really get this one. You are not leaving the website when using mailto:with window.location.href

我没有真正得到这个。使用mailto:with时您不会离开网站window.location.href

回答by Guillaume

The window.location.hrefsolution by AmShaegar works pretty well but it caused side effect in a complex application I have been developping.

AmShaegar的window.location.href解决方案效果很好,但它在我一直在开发的复杂应用程序中引起了副作用。

I finally came up with this solution one might be interested in:

我终于想出了一个人们可能感兴趣的解决方案:

$('<iframe src="mailto:[email protected]">').appendTo('body').css("display", "none");

See this plunker: http://plnkr.co/edit/J0LvQU?p=preview

看到这个plunker:http://plnkr.co/edit/J0LvQU?p=preview

回答by CpnCrunch

Just close the window after a short interval:

只需在短时间间隔后关闭窗口:

var mailto_link = 'mailto:'+email+'?subject='+subject+'&body='+body_message;
var win = window.open(mailto_link,'emailWindow');
setTimeout(function() { win.close() }, 500);

回答by Samuel Rossille

The blank tab is opened by window.open(). You don't need that.

空白选项卡由 打开window.open()。你不需要那个。

The syntax for a mailto link should be something like

mailto 链接的语法应该类似于

<a href="mailto:[email protected]?subject=Comments about the color blue">Contact Us</a>

See http://www.addressmunger.com/mailto_syntax_tutorial/for more details.

有关更多详细信息,请参阅http://www.addressmunger.com/mailto_syntax_tutorial/

回答by ezchx

Try naming the window (myWindow) and adding a close() command:

尝试命名窗口 (myWindow) 并添加 close() 命令:

<script>
    myWindow=window.open("mailto:[email protected]");
    myWindow.close();
</script>';

This should close the extra browser window and keep the email application open. At least it worked for me.

这应该关闭额外的浏览器窗口并保持电子邮件应用程序打开。至少它对我有用。

回答by Andrei Cristian Prodan

No, that strictly depends on how your browser handles new tabs. I've spent hours looking for a work around, solution, anything...

不,这完全取决于您的浏览器如何处理新标签。我花了几个小时寻找解决方法,解决方案,任何事情......

firefox: options -> tabs

firefox:选项 -> 标签

safari: preferences -> tabs

safari:首选项 -> 标签

回答by LeandroP

For the record:

作为记录:

create a anchor tag with the target attribute like this:

创建一个带有 target 属性的锚标记,如下所示:

<div>
    <a target="_self" href="mailto:[email protected];%[email protected]?subject=Mail%20sending&body=etc...">
        Send Mail
    </a>
</div>

回答by Laarni Cortez

<a className="prom-links-anchor" tabIndex="0" aria-label="Email" href={ "mailto:" + eid + "@yahoo.com"}   
   role="link" nocm="true" title={ "Email"} rel="email" data-cs={contentSource} data={resultPosition + 1} 
   onMouseDown={this.handleMouseDown.bind(this)} onKeyDown={this.handleKeyPressEvent.bind(this)} 
   onContextMenu={e=> e.preventDefault()}
  >
  <div className="icon-email" title="Email" href={"mailto:" + eid + "@yahoo.com"} rel="email" 
       data-cs={contentSource} data={resultPosition + 1} />
  <div className="icon-label-email" aria-hidden="true" href={"mailto:" + eid + "@yahoo.com"} 
       title={"Email"} rel="hc-email" data-cs={contentSource} data={resultPosition + 1}
    >Email</div>
</a>