12

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>
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
MistaBoodadee
  • 141
  • 1
  • 1
  • 5
  • There doesn't seem to be an `iframe` named "cake" in your HTML, but in general, `document.frames['cake'].location.href = what_ever;`. Or give an `id`for your `iframe` and say `document.getElementById('iframe_id').src ='what_ever';`. Also it's recommended to get familiar with [the elements you're working with](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe) ... – Teemu Sep 25 '15 at 20:33
  • I know, making an iframe named "cake" is what I want (intend) to make. – MistaBoodadee Sep 25 '15 at 22:35
  • You're asking how to access an iframe, not how to create a one ... – Teemu Sep 26 '15 at 05:42

2 Answers2

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>
Community
  • 1
  • 1
Sanket Bajoria
  • 748
  • 6
  • 18
2

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>
MistaBoodadee
  • 141
  • 1
  • 1
  • 5