在用iframe的時候,用一些場景,比如先將iframe中的頁面切換之后,才能進行一些操作,這些操作是針對切換后頁面的,這個時候就比較惡心了,因為iframe切換頁面的時刻,是在當前頁面中的所有js執行完之后,瀏覽器才根據新的地址location進行請求新切換的頁面,所以導致出現錯誤。
示例:
上面js過程,等到彈出33之后,i1這個iframe才進行請求到3.html。
示例:
<!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>無標題文檔</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>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</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才進行請求到3.html。