Looking to execute a function within an iframe from the parent window?
Follow this example:
Welcome.html
<!DOCTYPE html>
<html>
<head>
<title>parent window</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function() {
$("a#update_text_link").click(function(event) {
event.preventDefault();
$("iframe#iframe_id")[0].contentWindow.update_text(); // executing the function
});
});
</script>
</head>
<body>
<p><a href="#" id="update_text_link">Update text in iframe</a></p>
<iframe src="iframe.html" id="iframe_id"></iframe>
</body>
</html>
iframe.html
<!DOCTYPE html>
<html>
<head>
<title>iframe page</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script>
function update_text() {
$("h1").fadeOut(function () {
$(this).text("Updated text.").fadeIn();
});
}
</script>
</head>
<body>
<h1>Initial text.</h1>
</body>
</html>
Some browsers may not support this setup, especially when running it locally. Consider hosting this on a web server and accessing it through such browsers.
For more details (including legacy browser compatibility), refer to Invoking JavaScript code in an iframe from the parent page.