How to detect click inside iframe in JavaScript

Java packages and methods are filled with rules and methods. They allow the user to setup the defined codes and work on them. However, there are some exceptions. Most of these exceptions arise with respect to common clauses and boundaries. One of which is the iFrame interface.

In this iFrame interface, the method is used to embed another file within the existing HTML document. This is a very powerful utility and allows developers to facilitate addresses of different websites and sources to get a scrutinized access to that of the current web page. In many cases, the iFrame works out to work as a hyperlink generating code.

Inside this frame, however, there are certain commands, which may not work. The reasons for a function not working can be plenty. The click ( ) function does not work, because of an iFrame embedded web page within another web page. Thus, to detect or create a feasible event inside the iframe requires a set of different functions, viz. contents ( ) and load ( ).


<!DOCTYPE html>

<html lang="en">


<meta charset="utf-8">

<title>Detect Click into iFrame</title>

<script src=""></script>

<style type="text/css">


        width: 100%;

        height: 200px;

        border: 2px solid #ccc;



<script type="text/javascript">



        $(this).contents().on("mousedown, mouseup, click", function(){

            alert("Click detected inside iframe.");







    <iframe src=""></iframe>



In the above code, you can observe that the iFrame is created in the previous statements and the data, which includes width, height, and the border of the frame, is seen. Inside the script part, the document is merged into the ready ( ) function for further execution and the iFrame is loaded by load ( ) function concurrently.

With this function, the entire frame is taken into the function part and the calling of functions will feature the load part. Next comes the content ( ) function, which is designated to initialize the function of the mouse from up, down and click functions respectively. Since the contents are loaded with this keyword, the function (), redirects to the load function, which in return gives out the alert message of click detected inside the iFrame.

In the final part, the source iFrame is designated, right outside the script of the main function part. This is the way, where an iFrame function is tested with the help of load and content functions without the source of click ( ) function.

View Demo

Leave a Reply

Your email address will not be published. Required fields are marked *