簡體   English   中英

Edge&IE:創建iframe並訪問窗口對象后,權限被拒絕

[英]Edge & IE: Permission Denied after creating an iframe and accessing window object

在腳本中創建iframe並稍有延遲(setTimeout)並嘗試訪問窗口對象時,在Edge和IE上出現“權限被拒絕”錯誤。

它適用於所有其他瀏覽器,並且在IE和Edge上沒有延遲。

<script>
    function createIframe(win) {
        console.log('foo', win.foo);
        var width = 300;
        var height = 250;
        var id = 'ifr';
        var src = '';
        var iframeTemplate = '<iframe id="'+id+'" src="'+src+'" width="'+width+'" height="'+
            height+'"  marginheight="0" marginwidth="0" scrolling="NO" frameborder="0"></iframe>';
        win.document.write(iframeTemplate);
        var iframe = win.document.getElementById(id);
        console.log('foo', win.foo);
        if (iframe) {
            var doc = iframe.contentWindow.document;
            doc.write('<html><head></head><body><script>console.log("foo on parent", window.parent.foo);</scr'+ 'ipt></body></html>');
            doc.close();
        } else {
            console.log('Failed to create an iframe');
        }
    }
    window.foo = 'bar';
    setTimeout(function() {
        createIframe(window);
    }, 3000);

</script>

此代碼應打印:

foo bar
foo bar
foo on parent bar

但是它在Edge和IE的第二個console.log上引發錯誤“權限被拒絕”。

沒有setTimeout可以正常工作。

如果從iframe中刪除第二個console.log和acess window.parent.foo,則在Edge和IE上未定義

片段:

在Edge和IE上不起作用: https : //jsfiddle.net/vo2yrjft/

可在Edge和IE上使用: https : //jsfiddle.net/6cbfk1yr/

有什么解決方法嗎?

document.write是一種“不良做法”,它將阻止該頁面,您可以參考此線程以獲取詳細信息。

解決方法是,可以使用createElement('iframe')創建iframe,然后使用appendChild()插入元素。 下面是示例代碼,它可以在IE&Edge中正常運行:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <script>
        function prepareFrame(win) {
            console.log('foo', win.foo);
            var id = 'ifr';
            var ifrm = document.createElement('iframe');
            ifrm.setAttribute('src', '');
            ifrm.setAttribute('id', id);
            ifrm.setAttribute('marginheight', 0);
            ifrm.setAttribute('marginwidth', 0);
            ifrm.setAttribute('scrolling', 'NO');
            ifrm.setAttribute('frameborder', 0);
            ifrm.style.width = '300px';
            ifrm.style.height = '250px';
            document.body.appendChild(ifrm);
            var iframe = win.document.getElementById(id);
            console.log('foo', win.foo);
            if (iframe) {
                var doc = iframe.contentWindow.document;
                doc.write('<html><head></head><body><script>console.log("foo on parent", window.parent.foo);</scr' + 'ipt></body></html>');
                doc.close();
            } else {
                console.log('Failed to create an iframe');
            }
        }
            window.foo = 'bar';
            setTimeout(function() {
                prepareFrame(window);
            }, 3000);
    </script>
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM