I have an html file named first.html which has an iframe element to load another html file named second.html. How can we communicate between these two html files. I have tried to trigger an event from the second.html and to catch the same on first.html. But event propagation is not successful.
Here is the first.html file
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js" />
<script>
function onLoad(){
alert("onLoad");
var iframe = $('#iframeID').contents();
iframe.find("#button").on("SUBMIT",function(){
// business logic
alert("Clicked");
});
}
</script>
</head>
<body>
<iframe id="iframeID" onload="onLoad()" height="1000" width="1000" src = './second.html'></iframe>
</body>
</html>
Here is the second.html file
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js" />
<script>
function myFunction(){
$('#button').trigger("SUBMIT");
};
</script>
</head>
<body>
<button id="button" type="button" onclick="myFunction()">Click me</button>
</body>
</html>