从iFrame获取内容,错误在哪里?

时间:2012-12-10 19:17:54

标签: javascript html css jsp iframe

我想获取位于同一域中的多个URL的安全区域(Int​​ernet区域/本地Intranet)。我认为最好的解决方案是通过iFrame实现这一目标。 @Teemu。代码现在有效。非常感谢你。但到目前为止存在一个问题:它显示错误的文本。虽然页面位于受信任区域(本地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>

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

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[]是文档中“窗口”对象的集合。此代码只是检查,是否可以获得documentiframe的引用。当iframe的内容从跨源来源加载时,这是不可能的。