xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

how to get iframe dom in js All In One

how to get iframe dom in js All In One


// iframe.contentDocument == iframe.contentWindow.document
document.getElementById('myframe').contentWindow.document;

function GetDoc(x) {
    return x.contentDocument || x.contentWindow.document;
}

contentDocument

// const iframeDocument = document.getElementsByTagName("iframe")[0].contentDocument;

const iframeDocument = document.querySelector("#iframe").contentDocument;

iframeDocument.body.style.backgroundColor = "blue";

Same Origin

https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/contentDocument

contentWindow

// const iframeWindow = document.getElementsByTagName("iframe")[0].contentWindow;

const iframeWindow = document.querySelector("#iframe").contentWindow;

iframeWindow.document.body.style.backgroundColor = "blue";

https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/contentWindow

demos

CORS ⚠️

https://cdn.xgqfrms.xyz/iframe/same-origin-iframe.html

https://cdn.xgqfrms.xyz/iframe/iframe-single-testing.html

const iframeDocument = document.querySelector("#sina").contentDocument;
iframeDocument.body.style.backgroundColor = "blue";
// 'blue'

const iframeWindow = document.querySelector("#sina").contentWindow;
iframeWindow.document.body.style.backgroundColor = "red";
// 

https://codepen.io/webgeeker/pen/KJppBr


<!DOCTYPE html>
<html lang="zh-Hans">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="author" content="xgqfrms">
    <meta name="generator" content="VS code">
    <title>iframe single testing</title>
</head>
<body>
    <section>
        <h1>iframe single testing</h1>
        <iframe
            src ="https://www.sina.com.cn/"
            target="_self"
            style="border: 1px solid red;"
            height="500"
            width="600">
            <p>Your browser does not support iframes.</p>
        </iframe>
        <script>
            // iframe onload
            const getAllIframes = () => {
                let iframes = [...document.querySelectorAll(`iframe`)];
                console.log(`iframes.length =`, iframes.length);
                iframes.forEach(
                    (iframe, i) => {
                        if(i < 10) {
                            console.log(`iframe =`, iframe);
                        }
                        // iframe.contentDocument || iframe.contentWindow.document
                        if (iframe.contentDocument) {
                            // old IE
                            console.log(`iframe.contentDocument =`, iframe.contentDocument);
                            // get all links
                        }
                        if (iframe.contentWindow.document) {
                            // new Chrome
                            console.log(`iframe.contentWindow.document =`, iframe.contentWindow.document);
                            // get all links
                        }
                    }
                );
            };
            const removeAllIframesBlankLinks = () => {
                // iframe & virtualDOM bug
                let links = [...document.querySelectorAll(`a`)];
                console.log(`links.length =`, links.length);
                links.forEach(
                    (link, i) => {
                        if(i < 10) {
                            console.log(`link =`, link);
                        }
                        if (link.target) {
                            console.log(`link.target =`, link.target);
                            link.target = "_self";
                        }
                    }
                );
            };
            setTimeout(() => {
                getAllIframes();
            }, 10000);
            setTimeout(() => {
                removeAllIframesBlankLinks();
            }, 10000);
        </script>
    </section>
</body>
</html>


CORS

https://stackoverflow.com/questions/6170925/get-dom-content-of-cross-domain-iframe

libs ???

https://github.com/oyvindkinsey/easyXDM#readme

postMessage

https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

iframe & CSP

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe
https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/csp

https://stackoverflow.com/questions/926916/how-to-get-the-bodys-content-of-an-iframe-in-javascript

https://html.spec.whatwg.org/multipage/iframe-embed-object.html#dom-iframe-contentwindow
https://w3c.github.io/webappsec-feature-policy/#feature-policy
https://fetch.spec.whatwg.org/#concept-response-csp-list

https://www.w3schools.com/jsref/prop_frame_contentdocument.asp

CORS demo

window.frames[0].document;

https://cdn.xgqfrms.xyz/iframe/iframe-single-testing.html
https://cdn.xgqfrms.xyz/iframe/same-origin-iframe.html

iframe & HTTPS & CORS

https://iframe.xgqfrms.xyz/eapp/index.html#blog.sina.cn

refs

https://stackoverflow.com/questions/3999101/get-iframes-document-from-javascript-in-main-document

https://www.dyn-web.com/tutorials/iframes/refs/iframe.php
https://www.dyn-web.com/tutorials/iframes/refs/parent.php



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2019-01-24 00:27  xgqfrms  阅读(568)  评论(8编辑  收藏  举报