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

时间:2011-12-30 16:49:26

标签: javascript location uri document mailto

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

1 个答案:

答案 0 :(得分:3)

打开需要关闭的窗口并不是最佳选择。 如果您没有收到警告,可以像这样使用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>