mailto uri - javascript window.open 然后关闭,在 << 中仍然保持打开状态IE8

发布于 2024-12-24 16:17:40 字数 624 浏览 1 评论 0 原文

function SendInfo(href) {
    var subject= "Some information";
    var body = "I thought you might find this information interesting:\r\n\r\n<";
    body += document.location;
    body += ">";
    var uri = "mailto:?subject=";
    uri += encodeURIComponent(subject);
    uri += "&body=";
    uri += encodeURIComponent(body);
    win = window.open(uri);
    win.close();
}

在 FF、Chrome 和 IE9 中,新选项卡/窗口将按其应有的方式关闭。

然而,在 IE8 及更低版本中,会打开一个新窗口,并且用户会收到安全警告。

有没有更好的方法来解决这个问题来防止这些问题?

jsbin: http://jsbin.com/itazab

function SendInfo(href) {
    var subject= "Some information";
    var body = "I thought you might find this information interesting:\r\n\r\n<";
    body += document.location;
    body += ">";
    var uri = "mailto:?subject=";
    uri += encodeURIComponent(subject);
    uri += "&body=";
    uri += encodeURIComponent(body);
    win = window.open(uri);
    win.close();
}

In FF, Chrome and IE9, the new tab/window closes as it should.

However in IE8 and below, a new window opens and the user gets a security warning.

Is there a better way to approach this to prevent those issues?

jsbin: http://jsbin.com/itazab

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宫墨修音 2024-12-31 16:17:40

打开需要关闭的窗口并不是最佳选择。
如果您没有收到警告,您可以像这样使用 setTimeout:

win = window.open(uri);
setTimeout(function() { win.close();},1000);

相反,我建议您这样做

function SendInfo(href) {
  var subject= "Some information";
  var body = "I thought you might find this information interesting:\r\n\r\n<";
  body += href; // or document.location;
  body += ">";
  var uri = "mailto:?subject=";
  uri += encodeURIComponent(subject);
  uri += "&body=";
  uri += encodeURIComponent(body);
  return uri;
}

<a href="#" onclick="this.href=SendInfo(location.href)">Tell a friend</a>

It is not optimal to open a window that you need to close.
If you did not get a warning you could use setTimeout like this:

win = window.open(uri);
setTimeout(function() { win.close();},1000);

Instead I suggest you do this

function SendInfo(href) {
  var subject= "Some information";
  var body = "I thought you might find this information interesting:\r\n\r\n<";
  body += href; // or document.location;
  body += ">";
  var uri = "mailto:?subject=";
  uri += encodeURIComponent(subject);
  uri += "&body=";
  uri += encodeURIComponent(body);
  return uri;
}

<a href="#" onclick="this.href=SendInfo(location.href)">Tell a friend</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文