2016-10-16 40 views
0

我一直在嘗試訪問來自iframe的數據。我正在處理codeigniter。我花了整整一天的時間尋找解決方案,但沒有運氣。我不是javascript的專家,所以對我來說變得更加困難。請看看我的代碼。如何訪問iframe中的數據?

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();但沒有運氣。

+1

可能的重複[如何使用Javascript訪問iframe元素?](http://stackoverflow.com/questions/1452871/how-can-i-access-iframe-elements-with-javascript) –

+0

我已經訪問過該頁面仍然存在相同的問題。 –

+0

只是一個提示,我試圖解決一個問題,使用webinterface(它使用框架)幾天前,我用'window.frames ['name_of_the_frame']。文件' –

回答

1

對於您的問題已經有很多答案,谷歌「訪問iframe內的DOM」幾種解決方案,here is one

一般來說,如果iframe由於安全原因被加載到另一個域上,則您不能訪問iframe數據(否則,想象一下Facebook上的「Like」小部件,您可以加載它,然後竊取授權cookie用戶訪問您的網站...)

編輯:請注意,上述安全限制包括一些有趣的情景是你的網站從「www.domain.com/page.php」,並從「domain.com你的iframe中加載/iframe.php「,所以檢查出來。

+1

而不是指出回答完全相同的問題的答案,將問題標記爲重複。 –

+0

@CharlotteDunois感謝您的提示,我是新來的:) – Johnny

+0

你能解釋一下你的意思是另一個域名嗎? –