使用javascript函数在iFrame中加载页面

时间:2015-09-25 20:21:45

标签: javascript iframe

我尝试使用Javascript函数在名为" cake"的iframe中加载网页。我怎样才能做到这一点?

<html>
  <head>
  </head>
  <body>
    <button onclick=[function]>Load page in iframe</a>
  </body>
</html>

2 个答案:

答案 0 :(得分:10)

function changeUrl() {
    var site = "http://www.w3schools.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>

答案 1 :(得分:1)

这有效:

<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>