我想获取同一域中多个 URL 的安全区域(Internet 区域/本地 Intranet)。我认为最好的解决方案是通过 iFrame 获取此信息。 @提姆。代码现在有效。非常感谢。但到目前为止有一个问题:它显示了错误的文本。尽管该页面位于受信任区域(本地 Intranet)中,但他显示“不受信任”(Internet 区域)。知道为什么吗?
<html>
<head>
<script type="text/javascript">
function getZone() {
var text = new Array();
document.getElementsByClass('iframeNames');
var test;
for (var i = 0; i < document.frames.length; i++) {
try {
test = document.frame[i].contentWindow.document;
text[i] = "Trusted (Local Intranet)";
} catch (e) {
text[i] = "Untrusted (Internet Zone)";
}
} //end-for
var showText = getElementsByClass("zone");
for (var i = 0; i < showText.length; i++) {
setText(showText[i], text[i]);
};
} //end getZone()
function setText(showText, text) {
showText.innerHTML = text;
} //end setText
if (document.getElementsByClassName) {
getElementsByClass = function (classList, node) {
return (node || document).getElementsByClassName(classList);
};
}
</script>
</head>
<style type="text/css">
tr:nth-child(2n) {
background-color: #ddd;
}
.format {
background-color: #ffff;}
#formatierung {
margin-left: 10px;
margin-top: 30px;
font-family: Arial;
font-size: 13px;
}
</style>
<body>
<iframe src = "http://example.com" class = "iframeNames" width = "0"
height ="0"></iframe>
<iframe src = "http://example.com/index/" class = "iframeNames" width = "0"
height = "0"></iframe>
<script type = "text/javascript">
window.onload = function() {
getZone();
};
</script>
<div id = "formatierung">
<table width = "100%">
<tr class = "format"><td><h2>System</h2></td><td><h2>Check Security-Zone</h2></td></tr>
<tr><td>example1</td><td class = "zone"></td></tr>
<tr><td>example2</td><td class = "zone"></td></tr>
</table>
</div>
</body>
</html>
最佳答案
你可以试试这个:
var test;
for (var i = 0; i < document.frames.length; i++) {
try {
test = document.frames[i].document;
text[i] = "Trusted (Local Intranet)";
} catch (e) {
text[i] = "Untrusted (Internet Zone)";
}
}
使用此代码,您根本不需要 isTrustedIE()
。
正如我在之前的评论中所说,frames[]
是文档中“窗口”对象的集合。这段代码只是检查它是否可以在 iframe
中获取对 document
的引用。当 iframe
的内容是从跨域源加载时,这是不可能的。
关于javascript - 从iFrame中获取内容,错误在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13807627/