Reputation: 1252
Currently I've got a website with an iframe
loaded from another site into my website. The loaded iframed site requires a button, which has an id called 'accept', to be clicked. (cookies)
Of course the user should not click this button twice, so I want to click this button automatically once MY page is loaded.
Problem:
The loaded iframe
from the other site has no ID neither NAME tag.
How can I click this button on page load?
Code which I've tried, but does not work:
<script type="text/javascript">
$(document).ready(function(){
$("#accept").click();
});
</script>
I've readed on the internet it's possible to listen to an iframe
without an ID. But I have no clue on how to write this into code.
I've been searching google the whole day. I can't get a solution.
Hopefully someone can help me on my encountered problem. A fixed code with explaination/documentation would do it for me.
Best regards.
Upvotes: 2
Views: 1565
Reputation: 1252
It's not really a solution, but it's an answer on my question: SecurityError: Blocked a frame with origin from accessing a cross-origin frame
You can't access an with Javascript, it would be a huge security flaw if you could do it. For the same-origin policy browsers block scripts trying to access a frame with a different origin.
Upvotes: 1
Reputation: 23869
You can listen to the load
event of an iframe. I suppose there is only one iframe in your HTML page, so the following can work without any issue:
$(function() {
$('iframe').load(function() {
$('iframe').contents().find('#accept').click();
});
});
Note that you need to access the contents of the iframe using contents()
method.
Upvotes: 2