Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how i can auto click button inside ifrmae this button from other site

HTML
<pre><iframe id='dz' frameborder='0' scrolling='no' src='http://site/button.html'></iframe>

I use this code but not work

What I have tried:

<pre lang="java">$(document).ready(function(){

// set time out 5 sec

setTimeout(function(){

$('#dz').trigger('click');

}, 4000);

});
Posted
Updated 15-Jan-21 1:23am

1 solution

You can't. If you load a page from another site inside an <iframe>, code running in your page will have no access to it.

Imagine what would happen if it did: you visit a random website, which loads your (logged-in) bank site in an <iframe>, and automates the site to transfer all of your money to the site owner's account.

This is a security restriction. There is no way around it.

In addition, most sites will use a Content Security Policy[^], or the X-Frame-Options header[^], to prevent you from embedding their site in yours at all.
 
Share this answer
 
Comments
Prince_Hdz 15-Jan-21 7:33am    
hi
thank you very much
but its for same domain
<iframe id='dz' frameborder='0' scrolling='no' src='http://mysite/button.html'></iframe>
Prince_Hdz 15-Jan-21 7:38am    
i use this but not work too
var button = document.getElementById('dz');
setInterval(function() {   
    button.click()
},4000)
Richard Deeming 15-Jan-21 7:59am    
If it's a page from the same domain, and the embedded document has fully loaded, then you need to find the button within the embedded page and click that. Triggering the click event on the <iframe> won't do anything.

With jQuery:
$("#dz").contents().find("#yourButtonId").click();

.contents() | jQuery API Documentation[^]

Without jQuery:
document.getElementById('dz').contentDocument.getElementById('yourButtonId').click();

contentDocument - Web APIs | MDN[^]
Prince_Hdz 15-Jan-21 8:31am    
this help me thank thank thank you very much you made my day

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900