js中window.top, self, parent之间的关系

项目场景:

准备3个html页面,层级结构是 a.html <= b.html , <= c.html
a.html是最顶层父页面


问题描述:

a.html 中引入 b.html

<body>
  <iframe src="./b.html" frameborder="0" class="son" id="son1"></iframe>
</body>

a.html中定义变量aaaa=1

<script type="text/javascript">
  window.aaaa = 1
  console.log(top);
  console.log(window);
  console.log(window.top);
  console.log(window.self);
  console.log(window.parent);
  console.log(parent);
  console.log("-=-=-=-=-=-=-=-=-=-=-=-=");
</script>

b.html 中引入 c.html

<body>
  <iframe src="./c.html" frameborder="0" class="son" id="son2"></iframe>
</body>

b.html中定义变量aaaa=2

<script type="text/javascript">
  window.aaaa = 2
  console.log(top);
  console.log(window);
  console.log(window.top);
  console.log(window.self);
  console.log(window.parent);
  console.log(parent);
  console.log("-=-=-=-=-=-=-=-=-=-=-=-=");
</script>

c.html 中定义变量aaaa=3

<script type="text/javascript">
  window.aaaa = 3
  console.log(top);
  console.log(window);
  console.log(window.top);
  console.log(window.self);
  console.log(window.parent);
  console.log(parent);
  console.log("-=-=-=-=-=-=-=-=-=-=-=-=");
</script>

最终输出结果:

a.html

<script type="text/javascript">
  window.aaaa = 1
  console.log(top);    //aaaa = 1
  console.log(window);    //aaaa = 1
  console.log(window.top);    //aaaa = 1
  console.log(window.self);    //aaaa = 1
  console.log(window.parent);    //aaaa = 1
  console.log(parent);    //aaaa = 1
  console.log("-=-=-=-=-=-=-=-=-=-=-=-=");
</script>

在最顶层a.html中 所有window对象中顶层的aaaa都等于1

b.html

<script type="text/javascript">
  window.aaaa = 2
  console.log(top);    //aaaa = 1
  console.log(window);    //aaaa = 2
  console.log(window.top);    //aaaa = 1
  console.log(window.self);    //aaaa = 2
  console.log(window.parent);    //aaaa = 1
  console.log(parent);    //aaaa = 1
  console.log("-=-=-=-=-=-=-=-=-=-=-=-=");
</script>

b.html中 单独window对象window.self指向了b.html本身的window 对象,其余的top parent指向了父级window

c.html

<script type="text/javascript">
  window.aaaa = 3
  console.log(top);    //aaaa = 1
  console.log(window);    //aaaa = 3
  console.log(window.top);    //aaaa = 1
  console.log(window.self);    //aaaa = 3
  console.log(window.parent);    //aaaa = 2
  console.log(parent);    //aaaa = 2
  console.log("-=-=-=-=-=-=-=-=-=-=-=-=");
</script>

c.html中 单独window对象window.self 依然指向了c.html本身的window 对象。
topwindow.top 指向了最顶层的a.html
window.parent parent指向了b.html


所有在iframe中,
windowwindow.self 最终都是指向自己本身拥有的window
topwindow.top 都是指向最顶层的window,最开始的window。
parentwindow.parent 指向本级window父window不会指向最开始的祖先window


微信群大佬都在等着你

微信扫描二维码加入微信群,交流学习,及时获取代码最新动态。

`window.open()` 是 JavaScript 中用于打开新浏览器窗口或标签页的一个方法。它非常有用,在构建 Web 应用程序时经常会被使用到,比如当你想让用户点击某个链接后在一个新的页面中查看内容。 ### `window.open()` 的基本语法: ```javascript var windowObjectReference = window.open(strUrl, strWindowName[, strWindowFeatures]); ``` #### 参数解释: 1. **strUrl** (可选) - 指定要在新窗口加载的文档 URL。如果省略此参数,则将创建一个空白的新窗口。 2. **strWindowName** (可选) - 目标窗口的名字。这个名称可以作为 `<a>` 标签的目标属性值 (`target`) 使用;也可以设置为 `_blank`, `_self`, `_parent`, 或者 `_top` 来指定默认的行为方式。 3. **strWindowFeatures** (可选) - 包含了逗号分隔的一系列特性字符串列表,它们决定了新开窗口的各种属性,如宽度、高度等。如果不提供该参数,默认会采用系统设定的标准尺寸。 下面是一些常用的特性和其对应的含义: | 特性 | 描述 | |------------|----------------------------------------------------| | width | 新窗口的宽 | | height | 新窗口的高度 | | top | 窗口距离屏幕顶部的位置 | | left | 窗口距离屏幕左侧的位置 | | resizable | 是否允许调整大小 | | scrollbars | 显示滚动条 | 例如,如果你想弹出一个新的有固定大小且不允许用户改变其大小的小窗,并包含地址栏和状态栏,你可以这样做: ```javascript myWindow=window.open('http://example.com', 'newWin','width=400,height=300,resizable=no,location=yes,status=yes'); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值