iframe 做为html页面构成的基本元素之一,具有下面的特色html
iframe具有一些节点属性,以下:算法
为何要这么作?
在iframe中,你能够加载不一样的内容,这类内容不会被浏览器再一次进行解释,举个例子来讲:
若是你想嵌入一些特别的符号,你就能够和sandbox联合使用(例子中的amp就是一个特殊符号)
<iframe
sandbox
srcdoc="<p>hey that's earl's table. <p>you should get earl&amp;me on the next cover."
>
</iframe>
复制代码
allow-forms, allow-modals, allow-orientation-lock, allow-pointer-lock, allow-popups, allow-popups-to-escape-sandbox, allow-presentation, allow-same-origin, allow-scripts, allow-top-navigation, and allow-top-navigation-by-user-activation.
复制代码
好比你嵌入了一篇文章,这篇文章有全屏观看的操做
<iframe
src="http://article...."
>
</iframe>
// http://article....
<div
id='article'
onclick={handleFullClick}
>
// 省略文章内容
</div>
script:
const handleFullClick = () => {
const article = document.getElementById('article');
article.requestFullscreen();
}
复制代码
// 嵌入的iframe是否容许定位
<iframe src="https://maps.example.com/" allow="geolocation"></iframe>
复制代码
enum ReferrerPolicy {
"",
"no-referrer",
"no-referrer-when-downgrade",
"same-origin",
"origin",
"strict-origin",
"origin-when-cross-origin",
"strict-origin-when-cross-origin",
"unsafe-url"
};
复制代码
iframe是被浏览器的当前页面以第三方资源的形式嵌入的,若是想两者之间实现通讯,就须要经过postMessage浏览器
otherWindow.postMessage(message, targetOrigin, [transfer]);
复制代码
Example:安全
父窗口:
// 假定iframe的id = iframe
document.getElementById('iframe').contentWindow.postMessage('123', '*');
子窗口:
Window.addEventListener('message', ({ data }) => {
console.log('data');
// 向父窗口发送消息
if(window.parrent !== window.self) {
window.parrent.postMessage('456', '*');
}
})
复制代码