【问题标题】:Can a new page have a target="" name if not opened with a target attribute?如果没有使用 target 属性打开新页面,是否可以具有 target="" 名称?
【发布时间】:2014-08-13 09:01:07
【问题描述】:

我希望有两个相互链接的页面,但除非用户关闭它们,否则它们保持打开状态。我知道我可以说target="first"target="second",但是有没有办法让第一页的目标名称为“第一”,而无需单击链接打开它?

用法:

  • 通过输入地址(或从搜索引擎获取链接等)“首先”打开页面
  • 单击指向第二页的链接 - “第二”页在新选项卡中打开
  • 点击第二页中的链接 - 该链接将在“第一”选项卡中打开

目前我得到了“第一”页的两份副本:

  1. 名称:无,内容:首页
  2. 名称:“第二页”,内容:第二页
  3. 名称:“第一页”,内容:首页

我想重复使用第 1 页而不是打开第 3 页。

这可能吗?如何?这可以用任意数量的页面完成吗(即不依赖父级工作)?

【问题讨论】:

  • 为什么需要在新标签页中打开每个页面?

标签: html


【解决方案1】:

你可以使用这个javascript函数

window.open(url,"mypopup");

小提琴:http://jsfiddle.net/DfpG5/

参考这个答案: How to detect if user has already opened an url and redirect to same if already opened?

【讨论】:

  • 这不就等同于点击target="mypopup"的链接吗?
【解决方案2】:

以下代码可以满足我的要求:

target.html:

<!DOCTYPE html>
<html>
  <head>
    <title>Target</title>
    <script type="text/javascript">
      if (document.parentWindow.name == "") {
        document.parentWindow.name = "foobar";
      }
    </script>
  </head>
  <body>
    <a href="target2.html" target="barfoo">Second page</a>
  </body>
</html>

target2.html:

<!DOCTYPE html>
<html>
  <head>
    <title>Target 2</title>
    <script type="text/javascript">
      if (document.parentWindow.name == "") {
        document.parentWindow.name = "barfoo";
      }
    </script>
  </head>
  <body>
    <a href="target.html" target="foobar">First page</a>
  </body>
</html>

我还没有在很多浏览器中测试过,所以我不知道它的兼容性如何。

【讨论】:

  • 看来windowdocument.parentWindow更兼容
猜你喜欢
  • 1970-01-01
  • 2011-08-05
  • 1970-01-01
  • 2016-02-12
  • 2022-12-03
  • 2016-02-07
  • 2020-08-09
  • 1970-01-01
  • 2015-02-16
相关资源
最近更新 更多