dpjtn48868 2016-10-16 13:18
浏览 73
已采纳

如何访问iframe中的数据?

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.

  • 写回答

1条回答 默认 最新

  • douti9286 2016-10-16 13:24
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 arbotix没有/cmd_vel话题
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据