Reputation: 361
I have a page where I modded an app to prepopulate a number of fields. I would like to automatically have the 'submit' button be pressed/submitted when the page loads. I have tried things like:
<script type="text/javascript">
function autoclick() {
document.getElementById('buttonid').click();
}
</script>
with
<body onload="autoclick()">
But it does not work.
Any advice would be greatly appreciated!
Thanks
(It is the iframe on this site: http://abraxas.pw)
Upvotes: 0
Views: 4859
Reputation: 28387
I see that your iframe is in the same domain and hence it will possible for you as the cross-domain security may not apply.
Give your iframe
an id
. And then:
document.getElementById('iframeName').contentWindow.document.getElementById("buttonid").click()
More info here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#Scripting
Make sure that the DOM is fully loaded before you fire your Javascript. Wrap this code into body
load or iframe
load.
If the iframe is in the same domain and is in your control, you don't need to do all this. Just fire the click from domloaded
of jQuery, or place your code at the end (just before body ends). It will work.
Seeing your code, you have defined the function in the head
and are calling it at body
load. Place the function and the call at the end of the body.
Upvotes: 1
Reputation: 674
Content coming from different domain in iframe can't be controlled in your page using javascript. Browser treats iframe as a different window so you've no access over it. it's something like trying to access content of different window open in your browser from your own window(which is obviously not possible)
Upvotes: 0
Reputation: 509
You cannot do it in iframe for security reasons: http://pipwerks.com/2008/11/30/iframes-and-cross-domain-security-part-2/
Upvotes: 0