Radu
Radu

Reputation: 626

Detecting when an iframe is clicked

I am using the following code in order to check if an iframe has been clicked:

<script>
            focus();
            var listener = addEventListener('blur', function() {
                if(document.activeElement = document.getElementById('rhfrm_46861_71019_0')) {
                    $.ajax({ url: 'execute.php',
                     data: {action: 'test'},
                     type: 'post'
                    });
                }
                removeEventListener(listener);
            });
            </script>

However when I'm switching tabs, or I'm minimizing the window, the code still gets executed. How can I make it so that the ajax function is being called only when the user clicks the iframe?

Upvotes: 0

Views: 61

Answers (1)

brroshan
brroshan

Reputation: 1650

A simple answer to your question, "Detecting when an iframe is clicked" you can try something like this:

$('iframe').on('click', function(e) { 
      console.log('iframe clicked');
});

Remember that it won't work if the iframe is from a different domain than the host page.

Upvotes: 1

Related Questions