dpjtn48868 2016-10-16 13:18
浏览 72


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>
<input type="button" value="click me" onclick="run()">
    run() {
        var iframedoc = document.getElementsByTagName('iframe')[0].contentWindow.document;
        var inputs = iframedoc.getElementById('text');

home.php controller

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

upload-area.php view file

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

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 我用C语言easyx图形库绘制了一个3d游戏方框透视,但进入游戏时候鼠标准星对准方框边缘 鼠标光标就会弹出来这是啥情况怎样让光标对准绘制的方框点击鼠标不弹出光标好烦这样
    • ¥20 用Power Query整合的问题
    • ¥20 基于python进行多背包问题的多值编码
    • ¥15 相同型号电脑与配置,发现主板有一台貌似缺少了好多元器件似的,会影响稳定性和使用寿命吗?
    • ¥15 要求编写稀疏矩阵A的转置矩阵的算法
    • ¥15 编写满足以下要求的停车场管理程序,设停车场只有一个可停放n辆车的狭窄通道且只有一个大门可供车辆进出。
    • ¥15 C语言:数据子序列基础版
    • ¥20 powerbulider 导入excel文件,显示不完整
    • ¥15 用keil调试程序保证结果进行led相关闪烁
    • ¥15 paddle训练自己的数据loss降不下去