admin管理员组文章数量:1198384
Within my webpage I have an iframe like so:
<iframe src="thispage.html" width="100%" height="600" frameBorder="2"></iframe>
(The iframe is of a page on the same site...)
My question is, is it possible to use javascript on the page that has the iframe to control the 'thispage.html' (like use javascript functions?)
Within my webpage I have an iframe like so:
<iframe src="thispage.html" width="100%" height="600" frameBorder="2"></iframe>
(The iframe is of a page on the same site...)
My question is, is it possible to use javascript on the page that has the iframe to control the 'thispage.html' (like use javascript functions?)
Share Improve this question edited Oct 21, 2013 at 15:32 Jacob Mattison 51.1k11 gold badges108 silver badges128 bronze badges asked Oct 21, 2013 at 15:31 Ilan KleimanIlan Kleiman 1,2095 gold badges19 silver badges38 bronze badges 2- thispage.html is the main html file or the file you are loading into iframe. – Bharath R Commented Oct 21, 2013 at 15:33
- related question – Blazemonger Commented Jun 5, 2014 at 18:49
2 Answers
Reset to default 17Yes you can. Say your iframe
's ID is 'myIFrame'
<iframe id="myIFrame" src="thispage.html"
width="100%" height="600"
frameBorder="2">
</iframe>
Then, add the following in your JavaScript:
// Get the iframe
const iFrame = document.getElementById('myIFrame');
// Let's say that you want to access a button with the ID `'myButton'`,
// you can access via the following code:
const buttonInIFrame = iFrame.contentWindow.document.getElementById('myButton');
// If you need to call a function in the iframe, you can call it as follows:
iFrame.contentWindow.yourFunction();
Hope it helps :)
Yes, if you give your iframe an ID, for example
<iframe src="thispage.html" width="100%" height="600" frameBorder="2" id="MyIframe"></iframe>
You can access the inner window like this:
var iw = document.getElementById("MyIframe").contentWindow;
So you can call its functions e.g.
iw.theFunctionInsideIframe();
And you can locate the DOM elements like
var anElement = iw.document.getElmentByID("myDiv");
本文标签: Control iframe content with javascripthtmlStack Overflow
版权声明:本文标题:Control iframe content with javascripthtml - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738572637a2100690.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论