iframe
嵌入方法
https://www.fly63.com/article/detial/343
https://blog.csdn.net/weixin_38362146/article/details/74909685
<iframe src="included.html" width="450" height="400" >
<a href="included.html">你的浏览器不支持iframe页面嵌套,请点击这里访问页面内容。</a>
</iframe>iframe加载本地HTML
https://www.cnblogs.com/googlegis/p/15071446.html(是vue的而不是原生js的)
法2
https://code-examples.net/zh-CN/q/9eee8f
当iFrame从本地html文件加载本地html文件时,未来版本的Chrome可能是否支持contentWindow/contentDocument?
当我在本地html中加载另一个本地html文件在iFrame(frame.src =)在Chrome中时,我无法通过contentWindow或contentDocument访问它的文档(即使验证contentWindow是文档本身)
这是由于Chrome限制称为同源政策。 默认情况下,本地文件被认为属于不同的来源,不允许访问另一个。 要解决此问题,可以使用--allow-file-access-from-files命令行开关启动Chrome应用程序。WebView loadUrl加载文件总结,setAllowFileAccess、setAllowContentAccess、setAllowFileAccessFromFileURLs等
法3:有启发
https://code-examples.net/zh-CN/q/111e786
法4:https://zhuanlan.zhihu.com/p/359012129
方法5:利用 javascript 动态加载
<iframe id="foo" />
<script>
var iframe = document.getElementById('foo'),
iframedoc = iframe.contentDocument || iframe.contentWindow.document;
iframedoc.body.innerHTML = 'Hello world';
</script>https://wxnacy.com/2018/01/30/iframe-load-html-code/ (可用但显示空白)
参考
http://cn.voidcc.com/question/p-alnxzodw-da.html(没啥用)
Html code as IFRAME source rather than a URL没用
错误处理
显示空白:
https://blog.csdn.net/yumon321/article/details/82995399 (哟启发)
https://blog.csdn.net/qq_38719039/article/details/108397305
js加载iframe出现空白
iframeID 是加载的 Iframe ID号,修改成自己用的即可。
<iframe name="frames" id="iframeID" src=""/>
setTimeout("document.frames['"+iframeID+"'].location.href='"+iurls+"';",0);
//或
setTimeout("document.frames['"+iframeID+"'].location.reload();",0);X-Frame-Options may only be set via an HTTP header sent along with a document. It may not be set inside .
设置 meta 标签是无效的!例如 <meta http-equiv="X-Frame-Options" content="deny"> 没有任何效果。不要这样用!只有当像下面示例那样设置 HTTP 头 X-Frame-Options 才会生效。
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/X-Frame-Options
和我类似的坑:
https://blog.csdn.net/weixin_43589225/article/details/93884488(没用)
关闭iframe
无法“关闭”当前的iFrame,但是您可以告诉父级操纵dom并使它不可见。
在IFrame中:
parent.closeIFrame();在父母中:
function closeIFrame(){
$('#youriframeid').remove();
}全屏显示iframe
html
<div class = "settingsMain">
<iframe id = "settingsIframe" src="settings/index.html" allowfullscreen></iframe>
</div>全屏显示
#settingsIframe {
width: 100%;
height: 100%;
position: absolute;
top: 0;
}
.settingsMain {
position: absolute;
width: 100%;
padding-top: calc(100% * 高度/宽度)
}获取iframe的父页面URL
当iframe与父页面不属于同一个域名时,像上面的获取会因为安全策略原因而失败。在nczonline的一篇文章上看到一种方法,使用document.referrer。
方法很简单,通过parent != window检测iframe与父页面是否同源,当不同源时,使用document.referrer,否则使用parent.location.href或者top.location.href;
function getParentUrl() {
var url = null;
if (parent !== window) {
try {
url = parent.location.href;
}catch (e) {
url = document.referrer;
}
}
return url;调用子frame方法:
document.getElementById("IframeID").contentWindow.fn();js中iframe之间获取DOM和方法
iframe子页面获取父页面元素
//这个是获取父页面document中的对象;
window.parent.document.getElementById();
//获取父页面js中的方法:xxxx();
window.parent.xxxx();在父页面中获取iframe子页面中的元素:
var child = document.getElementById("mainFrame").contentWindow; //mainFrame这个id是父页面iframe的id
//获取子页面中的document对象;
child.document.getElementById();
//获取子页面的方法:xxxx();
child.xxxx();
//也可以连起来写
document.getElementById("mainFrame").contentWindow.xxxx();