更新时间:2023-02-18 11:12:06
在线编辑器上使用iframe是很困难的,因为它是沙箱环境,在正常情况下。作为有效的测试,您可以输入 http://example.com
将其列入白名单。
UPDATE
新增 PLUNKER ,因为SO沙箱iframes。
编辑
我添加了另一种方法来操纵你可能感兴趣的iframe,它只涉及HTML,没有JS。注意example.com中的 anchor
。基本上所有你需要做的是以下内容:
名称
属性到该iframe(我总是有id和名称相同)目标
属性值更改为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;} $ c>
< 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:
name
attribute to the iframe (I always have id and name the same)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>