How to Override an IFrame in JavaScript
- 1). Create a new HTML file in an editor or using the Notepad. For example, type:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Overriding iFrame</title>
</head>
<body>
</body>
</html> - 2). Add an iFrame to the Web page and assign an ID to it. Add a button to the Web page to override the iFrame. For example, between the HTML "<body>" tags, type:
<form><input type="button" name="override" value="Override" onclick="changeIFrame()" /></form>
<hr/>
<iframe src="http://www.example.com"></iframe> - 3). Create a JavaScript function to override the contents of the iFrame and insert it between the "<head>" tags in the HTML document. For example, type:
<script type="text/javascript">
function changeIFrame() {
var cur = document.getElementById("frame1").src;
if (cur.match(/yahoo/))
document.getElementById("frame1").src = "http://google.com";
else
document.getElementById("frame1").src="http://yahoo.com";
}
</script>
Source...