如何访问iframe中的数据?(How to access data within an iframe?)

编程入门 行业动态 更新时间:2024-10-28 12:17:38
如何访问iframe中的数据?(How to access data within an iframe?)

我一直在尝试从iframe访问数据。 我正在研究codeigniter。 我花了一整天时间寻找解决方案,但没有运气。 我不是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(); 但没有运气。

I have been trying to access data from an iframe. I am working on codeigniter. I have spent my whole day searching for the solution, but no luck. I am not an expert in javascript, so it is becoming more difficult to me. Please have a look at my code.

home-upload-dialog.php view file

<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 controller

upload_area() { $data['count'] = $count; $this->load->view('upload-area', $data); }

upload-area.php view file

<!DOCTYPE html> <html> <head> <title>Title here</title> </head> <body> <input type="text" name="text" id="text" value="someValue"> </body> </html>

I get the error saying Uncaught TypeError: Cannot read property 'contentWindow' of undefined There might be things I overlooked. Any help would be appreciated !

I also tried $('iframe').contents().find('#text').html(); but no luck.

最满意答案

你的问题已经有很多答案,google“在iframe中访问DOM”有几个解决方案, 这里有一个 。

一般情况下,如果出于安全原因将iframe加载到另一个域上, 则无法访问iframe数据(否则想象来自Facebook的“Like”小部件,您可以加载它然后窃取访问您网站的用户的授权cookie。 。)

编辑 :请注意,上面的安全限制包括有趣的情况,如您的网站从“www.domain.com/page.php”加载,您的iframe从“domain.com/iframe.php”加载,所以检查出来。

There are already many answers to your questions, google "access DOM inside iframe" for several solutions, here is one.

In general remember that you cannot access the iframe data if the iframe is loaded on another domain for security reasons (otherwise imagine the "Like" widget from Facebook, you could load it and then steal the authorization cookie of the users visiting your website...)

EDIT: Note that the security limitation above includes funny situations like your website is loaded from "www.domain.com/page.php" and your iframe from "domain.com/iframe.php", so check that out.

更多推荐

本文发布于:2023-08-01 16:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1362069.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   iframe   data   access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!