本文介绍了使用JavaScript更改< iframe>的来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码:

 <!DOCTYPE html> 
< html>
< head>
< script>
var URL = prompt(在此插入URL,http://www.example.com); //向用户询问URL
< / script>
< / head>
< body>
< iframe onload =this.src = URLheight =610pxwidth =1320id =window>< / iframe>
< / body>
< / html>

我试图使文件加载到< iframe> ; ,但是当它完成URL的加载时,它会重载,因为 onload 属性。我应该使用另一个属性吗?

解决方案

在线编辑器上使用iframe是很困难的,因为它是沙箱环境,在正常情况下。作为有效的测试,您可以输入 http://example.com 将其列入白名单。


$ b

UPDATE



新增 ,因为SO沙箱iframes。



编辑

我添加了另一种方法来操纵你可能感兴趣的iframe,它只涉及HTML,没有JS。注意example.com中的 anchor 。基本上所有你需要做的是以下内容:


  1. 添加名称属性到该iframe(我总是有id和名称相同)
  2. 在锚点上,将目标属性值更改为iframe的名称值。

因此,在此演示中, {{{...}}} 就是诀窍。

 < a href =http:// 

example.com{{{target =site}}}> Example.com< / a>

function changeSrc(src){var iframe = document.getElementById('site'); iframe.src = src;} body {width:100vw ;身高:100vh; overflow:hidden;} section {height:100%;宽度:100%; overflow-y:auto;} < form id = formonchange =changeSrc(url.value);> <字段集> <图例>输入网址< /图例> < input id =url> < / fieldset>< / form>< a href =https://example.comtarget =site> Example.com< / a>< section> < iframe id =sitename =sitesrc =/width =100%height =100%frameborder =0>< / iframe>< / section>


Here's the code:

<!DOCTYPE html>
 <html>
  <head>
   <script>
 var URL = prompt("Insert URL here", "http://www.example.com"); //Asks user for URL
   </script>
  </head>
  <body>
   <iframe onload="this.src=URL" height="610px" width="1320" id="window"></iframe>
  </body>
</html>

I'm trying to make the file load a URL into <iframe>, but when it finishes loading the URL, it reloads because of the onload attribute. Is there another attribute I should use? Thanks in advance.

解决方案

It's difficult to use an iframe on an online editor because of the sandbox environment but it'll behave normal under normal conditions. As a valid test, you can enter http://example.com it's whitelisted.

UPDATE

Added a PLUNKER since SO sandboxes iframes.

EDIT

I added another way to manipulate the iframe you might be interested in. Itonly involves HTML, no JS. Notice the anchor to example.com. Basically all you need to do is the following:

  1. Add a name attribute to the iframe (I always have id and name the same)
  2. On the anchor, you change the target attribute value to the value of the iframe's name value.

So in this demo the part inside {{{...}}} is the trick. The brackets are added for emphasis do not include them into the code to use.

<a href="http://example.com" {{{target="site"}}}>Example.com</a>

<iframe id="site" {{{name="site"}}} src="/" width="100%" height="100%" frameborder="0"></iframe>

function changeSrc(src) {
  var iframe = document.getElementById('site');
  iframe.src = src;
}
body {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}
section {
  height: 100%;
  width: 100%;
  overflow-y: auto;
}
<form id="form" onchange="changeSrc(url.value);">
  <fieldset>
    <legend>Enter URL</legend>
    <input id="url">


  </fieldset>
</form>
<a href="https://example.com" target="site">Example.com</a>
<section>
  <iframe id="site" name="site" src="/" width="100%" height="100%" frameborder="0"></iframe>
</section>

这篇关于使用JavaScript更改&lt; iframe&gt;的来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:56