Js如何監視Iframe內部地址改變

最近在用jsunit來測試js代碼。html

測試對象中有一個針對iframe的操做。就是經過iframe.contentwindow.location.href屬性更新iframe的url。可是每次測試時候,iframe.contentwindow.location.href都是修改之前內容。緩存

開始覺得是同源域的狀況,沒法訪問。可是明明是能夠訪問的啊。測試

後來又懷疑是cache緩存問題。強行刷新iframe也沒用。url

本身作了一個例子。code

1.html
htm

<html>
<head>
<script>
var iframe = document.getElementById("frameArea");
alert(iframe.src);
iframe.contentwindow.location.href="b.html";
alert(iframe.src);
alert(iframe.contentwindow.location.href);
</script>
</head>
<body>
<iframe id="frameArea" name="frameArea" src="a.html"/>
</body>
<html>

a.html對象

<html>
<head>
</head>
<body>
AAAAAAAAAAAAAAAAAAAAAA
</body>
<html>

b.htmlblog

<html>
<head>
</head>
<body>
BBBBBBBBBBBBBBBBBBBBBBBB
</body>
<html>

發現只有在頁面刷新後,才能顯示b.html頁面內容。事件

最後在http://q.cnblogs.com/q/59280/找到結果。
ip

最後,我在測試代碼中,將assert判斷放到iframe的load事件中就解決了。

相關文章
相關標籤/搜索