在用iframe的時候,用一些場景,比如先將iframe中的頁面切換之后,才能進(jìn)行一些操作,這些操作是針對切換后頁面的,這個時候就比較惡心了,因為iframe切換頁面的時刻,是在當(dāng)前頁面中的所有js執(zhí)行完之后,瀏覽器才根據(jù)新的地址location進(jìn)行請求新切換的頁面,所以導(dǎo)致出現(xiàn)錯誤。 示例: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標(biāo)題文檔</title>
<script type="text/javascript">
function test()
{
alert('11');
i1.location="3.html";
alert('22');
alert('33');
}
</script>
</head>
<body>
<iframe id="i1" name="i1" src="2.html" height="400" width="400" ></iframe>
<input type="button" value="切換" onclick="test()" />
</body>
</html>
上面js過程,等到彈出33之后,i1這個iframe才進(jìn)行請求到3.html。