2

So I'm trying to access an object in an iframe within my script printed below:

<script>document.getElementsByName("submission")[0].click();</script>

This works fine without the iframe, but as stated by question I'm unable to access this object when it's contained within Iframe!

What I've tried so far is giving the IFrame an ID and doing this:

<script>window.frames['NAME'].document.getElementByName("submission")[0].click();</script>

Why does this not work? And what should work?

David Ekermann
  • 87
  • 3
  • 13

2 Answers2

0

Use:

document.getElementById('iframeName').contentWindow.document.getElementById("submission")[0].click()

More info here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#Scripting

Edit:

See demo fiddle: http://jsfiddle.net/cqq96/

Security Note:

If the content of the iframe is from a different domain, then you may not be able to access depending on the security levels. e.g. read up on X-FRAME-OPTIONS and its implications.

Abhitalks
  • 27,721
  • 5
  • 58
  • 81
0

The problem was that the javascript was loaded before the DOM so by loading the script after using the thingi was the solution :)

Kind Regards!

David Ekermann
  • 87
  • 3
  • 13