首页 > 解决方案 > 框架集和 javascript 变量交互

问题描述

我知道框架集并不完全是尖端技术,但我不得不调试一个旧应用程序并且遇到了一个意想不到的行为。

这里有一些简单的 HTML 页面来展示这种行为。

索引.html:

<html>
  <frameset rows="22,*" border="0">
    <frame name="menu" src="menu.html" marginheight=0 marginwidth=0>
    <frame name="submenu" src="content1.html">
  </frameset>
</html>

菜单.html:

<html>
  <body>
    <table width=100% border=0 spacing=1>
      <tr>
        <td><a href="content1.html" target="submenu">Content 1</a></td>
        <td><a href="content2.html" target="submenu">Content 2</a></td>
      </tr>
    </table>
  </body>
</html>

内容1.html:

<html>
  <body>
    <p>Leverage agile frameworks to provide a robust synopsis for high level overviews. Iterative approaches to corporate strategy foster collaborative thinking to further the overall value proposition. Organically grow the holistic world view of disruptive innovation via workplace diversity and empowerment.</p>
  </body>
</html>

内容2.html:

<html>
  <body>
    <p>Seasonal, whipped java aftertaste lungo ristretto, a turkish chicory affogato saucer. Medium, spoon et skinny americano cappuccino lungo dripper.</p>
  </body>
</html>

行为是预期的:当我们单击菜单链接时,子菜单的内容会更新。

现在,我在 content2.html 的正文中添加一行:<script>var name = "why" ;</script>

如果我点击“内容 2”链接,内容会刷新,但从现在开始,当我点击任何链接时,框架的内容会在新选项卡中打开。我可以使用任何现代浏览器(Chrome、Firefox、Edge 等)重现此行为:name“主”脚本中的 javascript 变量会更改框架集的行为。

有人对此有解释吗?我不认为这name是一个保留关键字?为什么这个新标签打开而不是一个损坏的脚本?

标签: javascripthtmlframeset

解决方案


var name,如果用作全局变量,则与 相同window.name

window.name是保存框架名称的属性。

如果您将名称更改为不匹配的名称target(例如 from submenuto why),则没有匹配的目标,因此必须创建一个新目标。


推荐阅读