admin管理员组文章数量:1026373
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 being 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.
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 being 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 Possible duplicate of How can I access iframe elements with Javascript? – Charlotte Dunois Commented Oct 16, 2016 at 13:27
- I already visited that page and still have the same problem. – Rabin Lama Dong Commented Oct 16, 2016 at 13:33
-
Just a tip, I tried to fix an issue with a webinterface (which uses frames) a few days ago, I used
window.frames['name_of_the_frame'].document
– Charlotte Dunois Commented Oct 16, 2016 at 13:36 -
It still says
cannot read property 'document' of undefined
There might be small silly mistakes in my codes. But I have tried many options with no result. – Rabin Lama Dong Commented Oct 16, 2016 at 13:43 -
1
Your
iframe
needs to have aname
attribute. – Charlotte Dunois Commented Oct 16, 2016 at 13:44
1 Answer
Reset to default 3There 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./page.php" and your iframe from "domain./iframe.php", so check that out.
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 being 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.
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 being 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 Possible duplicate of How can I access iframe elements with Javascript? – Charlotte Dunois Commented Oct 16, 2016 at 13:27
- I already visited that page and still have the same problem. – Rabin Lama Dong Commented Oct 16, 2016 at 13:33
-
Just a tip, I tried to fix an issue with a webinterface (which uses frames) a few days ago, I used
window.frames['name_of_the_frame'].document
– Charlotte Dunois Commented Oct 16, 2016 at 13:36 -
It still says
cannot read property 'document' of undefined
There might be small silly mistakes in my codes. But I have tried many options with no result. – Rabin Lama Dong Commented Oct 16, 2016 at 13:43 -
1
Your
iframe
needs to have aname
attribute. – Charlotte Dunois Commented Oct 16, 2016 at 13:44
1 Answer
Reset to default 3There 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./page.php" and your iframe from "domain./iframe.php", so check that out.
本文标签: javascriptHow to access data within an iframeStack Overflow
版权声明:本文标题:javascript - How to access data within an iframe? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745642945a2160854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论