javascript - 测试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中框架的存在吗?
解决方案
我尝试使用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...");
}
推荐阅读
- java - 使用 Java 8 Streams 完成删除循环
- ios - AVPlayer 播放完歌曲后,如何隐藏 Remote Command Center?
- jquery - Coldfusion 在不重新加载页面的情况下动态更改输出变量
- opengl-es - 为什么我会在空对象引用上获得 surfaceTexture.updateTexImage()?
- flutter - Flutter MobX Observer 不会在 @action 时触发
- export - Dynamics 365 Finance and Operations Data Export 实体未显示?
- c# - C# 中 DataTable 中每个单元格的附加/自定义属性
- amazon-web-services - CloudFormation 更改集阻塞空 CodeUri:在 SAM 模板中
- xml - 如何使用 XQuery 进行替换
- javascript - 如何使用 AJAX 从数据库中检索数据并将结果保存在变量中?