2011-12-09 51 views
1

我想了解iframe的歷史。我已經創建到本地主機服務器上的頁面A和B.使用iframe我首先加載頁面A,然後動態地將iframe src更改爲頁面B.這是否意味着history.length = 2,因爲訪問了兩個不同的URL?它在沒有iframe的情況下嘗試。但是,通過使用iframe我只能得到值1返回?如何訪問iframe history.length

<body> 
<div> 
    <iframe src='/pageA.php' id='myframe' onload='checkHistory()' ></iframe> 
</div> 
</body> 
</html> 
<script language="javascript" type="text/javascript" > 
    function checkHistory() 
    { 
      document.getElementById('myframe').src='/pageB.php'; 
      alert("Number of URLs in history list: " + history.length); 
    } 
</script> 

我是否正確訪問history.lenght值的iframe或不iframe的不得不像document.getElementById('myframe').history.length,而不是一般的history.length屬性來訪問?

這讓我感到困惑。我曾嘗試在打開頁面B和比較值之前存儲history.length值,但仍然沒有運氣。 iframes必須有一種方式來存儲與瀏覽器選項卡窗口相同的頁面訪問歷史記錄嗎?

回答

-1

每個iframe作爲它自己的實體存在:窗口和文檔本身。 iframe的歷史與父母的歷史沒有關係;加載到iframe中的頁面不會計入父級的歷史記錄。您可以通過訪問iframe的自己的歷史:(這裏假設你的iframe從同一個域父加載,如你的例子說明)

document.getElementById("myframe").contentWindow.history.length 

+1

什麼樣,如果iframe的內容是從其他領域? –

+0

在這種情況下,瀏覽器的安全模型不會公開這些詳細信息... – Bosh

+0

這是不正確的。 iframe中的導航確實會添加到父級的history.length值,並且如果最近的導航位於該幀內,則返回將首先導航回iframe的歷史記錄。然而,你是正確的,歷史的細節被掩蓋了。 –