【问题标题】:Testing for the existence of a frame in Edge测试Edge中是否存在框架
【发布时间】:2020-09-24 20:59:47
【问题描述】:

我有以下测试适用于我测试过的所有浏览器,除了 Microsoft 旧版 Edge。

        var frame_name = window.parent.frames[1].name;

        if (window.parent[frame_name].frames[1].frames[1]) {
           //Edge thinks this is true, 
        }

在 legacyEdge 中,即使它是错误的,它也会进入那里。在 Chrome 等中,它认为这是不正确的。如果我查看 Edge 调试器工具中的值,我会看到:

window.parent[frame_name].frames[1].frames[1]   [object Window]
__proto__   [object WindowPrototype]
{error} Permission denied

有什么方法可以测试 Edge 中是否存在框架?

【问题讨论】:

    标签: javascript microsoft-edge


    【解决方案1】:

    我尝试用 MS Edge legacy 44.18362.449.0 版本进行测试,发现代码在其中运行良好。

    测试代码:

    <!DOCTYPE html>
    <html>
    <head>
    
    </head>
    <body>
    
    <h1>The iframe element</h1>
    
    <iframe src="http://your_site/childpage.html">
    </iframe>
    <script>
    if (window.parent.frames[1].frames[1]) {
       //Edge thinks this is true, 
       alert("found...");
       console.log(window.parent.frames[1].frames[1]);
    }
    else
    {
    alert("not forund...");
    }
    </script>
    </body>
    </html>
    

    输出:

    您可能由于缓存而面临此问题。我建议你尝试清除缓存并再次尝试测试代码。

    如果页面上只有 1 个 iframe,那么您可以使用下面的代码。

    if (window.parent.frames[0]) 
    {
       
    }
    

    您也可以尝试通过 ID 或名称查找 iframe,如下所示。

    <iframe src="http://your_site/childpage.html" id="frame1">
    </iframe>
    
    var element =  document.getElementById('frame1');
    if (typeof(element) != 'undefined' && element != null)
    {
       alert("found...");
    }
    

    【讨论】:

    • 我调整了代码以更接近我的实际代码。
    • 我还测试了您修改后的代码,但它在控制台中显示错误。发生错误是因为 frames[1].frames[1] 不可用。所以 Edge 浏览器仍然显示正确的结果。我想与您确认您使用哪个版本的 Edge 旧版浏览器进行此测试?
    猜你喜欢
    • 2019-04-19
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多