Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  20] [ 2]  / answers: 1 / hits: 28782  / 11 Years ago, mon, october 21, 2013, 12:00:00

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?)


More From » html

 Answers
12

Yes 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 :)


[#74832] Sunday, October 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gianni

Total Points: 307
Total Questions: 104
Total Answers: 96

Location: South Georgia
Member since Sun, Aug 8, 2021
3 Years ago
;