0

I am trying to click over an iframe, where there is a button, by clicking dynamically on certain coordinates. I created a button which dinamically clicks on the coordinates where that button is located, but it does not work.

In this JSFiddle sample I have the button clicks on the coordinate of x=30px and y=30px of the document where the bing logo is located, but when I click on the button, the Bing logo in iframe does not trigger.

<div class="button">Click Here</div>
<iframe src="https://www.bing.com/images" width="200" height="70"></iframe>



<script>
          function clicktransfer(x, y) {
                jQuery(document.elementFromPoint(x, y)).click();
            }

            $( ".gray" ).click(function() {
            clicktransfer(30, 30);
            });
</script>

Click here for JSFiddle

Any thoughts?

Bondsmith
  • 1,343
  • 3
  • 13
  • 27
  • Similar question: [How to detect a click inside of an iframe (cross-domain)?](http://stackoverflow.com/questions/29337304/how-to-detect-a-click-inside-of-an-iframe-cross-domain-aka-prevent-click-frau) – Yogi May 31 '15 at 20:28
  • Please explain what part I am doing wrong, I read this (cross-domain) post but it did not help. – Bondsmith May 31 '15 at 20:36

1 Answers1

0

Your script has accessed to the main document. The document in the iframe is a separate document with its own DOM structure. You may access it like this:

var doc = $('iframe')[0].contentWindow.document;

But with a cross-domain restriction. In other words, you CAN NOT access a document in the iframe that has a different origin.

AJG
  • 1,823
  • 1
  • 11
  • 7