获取对<a>

时间:2018-09-03 13:32:57

标签: javascript tabs

Can we get a reference to a tab which was opened by setting target attribute of anchor element to _blank.

I basically create a new hidden link and append it to a DOM and then I programmatically simulate click event on that element, is there a way to get a reference to a newly opened tab like we can with window.open by doing:

var newTab = window.open('http://www.google.com/');

EDIT: Before marking question as duplicate note that I'm unable to use window.open at all, hence this question.

1 个答案:

答案 0 :(得分:0)

在继续进行操作之前,有几个红色标记值得考虑:

  1. 您(作为开发人员)没有太多控制权来强制在浏览器选项卡中打开新链接。这是根据this answer

  2. 中用户的偏好设置
  3. window.open docs表示它返回null“如果无法打开窗口” 。您可以使用它 轻松 检查是否打开了新窗口。但是,这在不同的浏览器中可能不一致。

我建议您在沿这条路走之前先看看其他选项(如果有)。

一切顺利:)