在新选项卡中打开一个链接(在同一个窗口中),或者如果已经使用'href'打开它,则重新加载并聚焦它

时间:2015-02-21 14:17:13

标签: html hyperlink href

我想使用' href'打开一个链接。 (不使用javascript / jQuery),在同一窗口的新选项卡中。如果链接已经打开,它应该重新加载并关注它。

我如何编码来实现这一目标?

我试过了。

<a href="name.html" target="_blank">Click</a>

如果页面已经存在,则不会重新加载页面。它会一次又一次打开新标签中的链接。

有任何建议吗?

2 个答案:

答案 0 :(得分:5)

只需为目标指定一个唯一值,如下所示:

<a href="http://stackoverflow.com" target="mynewwindow">Always opens in the same window</a>

这是目标属性的默认行为。 _blank是少数特殊关键字之一,具有特殊行为,如HTML standard中所述。

请注意,标准并未讨论Windows,而是关于浏览上下文。浏览上下文通常是一个窗口或选项卡,但这是浏览器。

关于特殊关键字:

  • _blank表示始终使用新的浏览上下文(例如标签或窗口)
  • _self表示始终使用相同的浏览上下文
  • _parent表示使用父上下文(如果有)
  • _top表示如果有任何父母
  • ,请使用最上层的上下文

使用iframe,事情会有点复杂。

编辑:另请注意,用户第二次单击链接时,将不会重新加载目标页面。如果您需要刷新,则必须使用Javascript。

答案 1 :(得分:0)

如果您想始终保持弹出“聚焦”,您可以使用最初找到的技巧here。通过快速打开/关闭/打开它,我们确保它始终像新窗口并抓住焦点。

<a href="http://stackoverflow.com" onclick="
if(window.open('','reuse_win') != null) {
   window.open('','reuse_win').close();
}" target="reuse_win">Always opens in the same window and FOCUS</a>

在向目标窗口提交“表单”时也适用。

<form name=f1 method=post action=test.html>

  <input type=text name=name value='123'>

  <input type='submit' value='Submit' 

  onclick="if(window.open('','reuse_win') != null) {
       window.open('','reuse_win').close();
    }; this.form.target='reuse_win'; return true;">

</form>
相关问题