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

how to get iframe dom in js

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


// iframe.contentDocument == iframe.contentWindow.document

document.getElementById('myframe').contentWindow.document;


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

contentWindow

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

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

demo

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


Flag Counter

©xgqfrms 2012-2020

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


原文地址:https://www.cnblogs.com/xgqfrms/p/10312189.html