2

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.

Ronnie Oosting
  • 1,252
  • 2
  • 14
  • 35
  • 3
    Can't you simply select it with `$('iframe')` and trigger your click inside ? `$('iframe').contents().find('#accept').click()`. If you have multiple Iframes, just select the n-th one with `$('iframe').eq(n+1)` – Zenoo Feb 02 '18 at 14:51
  • @Zenoo Thank you. I received this error: `Uncaught DOMException: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "http://..." from accessing a cross-origin frame.`. Can this be solved? Or can this simply not? – Ronnie Oosting Feb 02 '18 at 15:00
  • 2
    https://stackoverflow.com/questions/25098021/securityerror-blocked-a-frame-with-origin-from-accessing-a-cross-origin-frame – The Alpha Feb 02 '18 at 15:02
  • Thank you @TheAlpha. Seems my idea is not working ;-). I'll edit my question. – Ronnie Oosting Feb 02 '18 at 15:03

2 Answers2

2

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.

31piy
  • 23,323
  • 6
  • 47
  • 67
1

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.

Ronnie Oosting
  • 1,252
  • 2
  • 14
  • 35