从 iframe 内部访问 iframe 名称 [英] access iframe name from inside iframe

查看:20
本文介绍了从 iframe 内部访问 iframe 名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我虽然这很简单,但我找不到答案.我需要知道如何从上述 iframe 中访问 iframe 的名称.我正在尝试这样的事情,但它不起作用.

I though this would be simple enough but I can't find the answer. I need to know how I can access an iframe's name from within said iframe. I was trying something like this but it's not working.

<iframe name="thename">
  <script type="text/javascript">
    alert(parent.name);
  </script>
</iframe>

推荐答案

你几乎是对的.在 frameiframe 上设置 name 属性会设置框架的全局 window<的 name 属性/code> 对象到该字符串.(不是 parent,它指的是拥有框架的文档的窗口.)

You were nearly right. Setting the name attribute on a frame or iframe sets the name property of the frame's global window object to that string. (Not parent, which refers to the window of the document that owns the frame.)

所以除非其他脚本故意改名,否则就这么简单:

So unless some other script has deliberately changed the name, it's as simple as:

1.html:
<iframe name="tim" href="2.html"></iframe>

2.html:
<script type="text/javascript">
    alert(window.name); // tim
</script>

这篇关于从 iframe 内部访问 iframe 名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆