首页 > 解决方案 > 测试Edge中是否存在框架

问题描述

除了 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中框架的存在吗?

标签: javascriptmicrosoft-edge

解决方案


我尝试使用MS Edge 旧版 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...");
}

推荐阅读