如何访问iframe中的数据
How to access data within an iframe?
我一直在尝试从iframe访问数据。我在研究编码器。我花了一整天的时间寻找解决办法,但运气不好。我不是javascript专家,所以这对我来说越来越难了。请看看我的代码。
home-upload-dialog.php
视图文件
<div id="popup-contents">
<iframe id="upload-frame" frameborder="0" scrolling="no" height="200" width="100%" src="<?php echo site_url('home/upload_area/' . $_REQUEST['count']);?>"></iframe>
</div>
<input type="button" value="click me" onclick="run()">
<script>
run() {
var iframedoc = document.getElementsByTagName('iframe')[0].contentWindow.document;
var inputs = iframedoc.getElementById('text');
console.log(inputs);
}
</script>
home.php
控制器
upload_area() {
$data['count'] = $count;
$this->load->view('upload-area', $data);
}
upload-area.php
视图文件
<!DOCTYPE html>
<html>
<head>
<title>Title here</title>
</head>
<body>
<input type="text" name="text" id="text" value="someValue">
</body>
</html>
我得到错误说Uncaught TypeError: Cannot read property 'contentWindow' of undefined
我可能忽略了一些东西。如有任何帮助,不胜感激!
我也试过$('iframe').contents().find('#text').html();
,但没有运气。
你的问题已经有很多答案,谷歌"访问DOM内iframe"的几个解决方案,这里是一个。
一般情况下,请记住,如果iframe加载到另一个域,则不能访问iframe数据(否则想象一下Facebook的"Like"小部件,您可以加载它,然后窃取访问您网站的用户的授权cookie…)
EDIT:请注意,上面的安全限制包括有趣的情况,如您的网站是从"www.domain.com/page.php"加载的,您的iframe从"domain.com/iframe.php",所以检查一下。
相关文章:
- 将数据发布到iframe(aspx)以在aspx页面的页面加载中捕获它
- 将数据发布到iframe是同步还是异步
- 如何在 iframe 中创建 HTML5 数据属性
- 如何将数据从注入的内容脚本传递到iframe
- 在带有数据的iframe中的JSONP中没有回调
- Iframe从父级获取/发布数据
- 传递数据 id 以引导模式并使用它 iframe url
- 如何将 iframe 数据传递到父窗口
- 带有 iframe 的多部分/表单数据
- Iframe 数据从父页面发布,如果服务器上发生异常,则重新加载父页面
- 使用 Jquery 将输入数据发送到 iframe 之外
- 在具有跨源的 iframe 中设置数据属性值
- 如何使用 jQuery 从父窗口中的 iframe 访问数据
- 是否可以在没有 Cookie 数据的情况下创建 iframe
- 从 iframe 访问数据
- Facebook是否可以获取用户的数据并在iframe中填充表单
- 从 iFrame 中每个页面上的 iFrame URL 获取数据
- iframe数据未正确打印
- 从Android WebView获取iframe数据
- 将Iframe数据拆分为不同的变量