I'm trying to use a Javascript function to load a web page in an iframe named "cake". How can I do that?
<html>
<head>
</head>
<body>
<button onclick=[function]>Load page in iframe</a>
</body>
</html>
I'm trying to use a Javascript function to load a web page in an iframe named "cake". How can I do that?
<html>
<head>
</head>
<body>
<button onclick=[function]>Load page in iframe</a>
</body>
</html>
Share
Improve this question
edited Sep 25, 2015 at 20:28
Paul Roub
36.4k27 gold badges85 silver badges94 bronze badges
asked Sep 25, 2015 at 20:21
MistaBoodadeeMistaBoodadee
1411 gold badge1 silver badge5 bronze badges
3
|
2 Answers
Reset to default 27
function changeUrl() {
var site = "https://www.google.com/";
document.getElementsByName('iFrameName')[0].src = site;
}
<html>
<head>
</head>
<body>
<button onclick="changeUrl()">Load page in iframe</button>
<iframe name="iFrameName"></iframe>
</body>
</html>
This worked:
<html>
<head>
<script type="text/javascript">
function changeUrl() {
var site = "1.wav";
document.getElementsByName('cake')[0].src = site;
}
</script>
</head>
<body>
<center>
<iframe src="http://www.w3schools.com" height=400 width=400 frameborder=0 name = "cake" style =""></></iframe>
<br>
<br>
<button onclick="changeUrl()">Load page in iframe</button>
</center>
</body>
</html>
iframe
named "cake" in your HTML, but in general,document.frames['cake'].location.href = what_ever;
. Or give anid
for youriframe
and saydocument.getElementById('iframe_id').src ='what_ever';
. Also it's recommended to get familiar with the elements you're working with ... – Teemu Commented Sep 25, 2015 at 20:33