2012-03-01 55 views
0

我在index.html文件上使用框架。在框架的每一頁上,我都有一個代碼檢查頁面是否在框架中,如果沒有,則重定向到index.html。將框架頁面重定向到index.htm中的特定框架

現在。我不僅要檢查頁面是否在框架中並重定向到index.html,而且還想在index.html中的一個框架中打開頁面。

我已經嵌入該代碼JS文件截至目前:

if (top.location == self.location) 
{ 
    top.location = 'index.html' 
} 

有沒有你可能知道的任何腳本?

回答

2

您需要讓子頁面中的代碼將其名稱附加到'index.html'中,例如

if (top.location == self.location) { 
    top.location = 'index.html?' + location.href; 
} 

...並把另一個JavaScript index.html頁面將問號後檢查的一部分,例如在:

<script type="text/javascript"> 
window.onload = function() { 
    //check if a query string is given 
    if (location.search && location.search.length > 1 
     && location.search.charAt(0) == "?") { 

     //load the URL to the frame 
     window.frames["myframe"].location.href = location.search.substring(1); 
    } 
} 
</script> 

順便說一句,你需要給你的目標框架一個名字是這樣的:

<frameset ... 
    <frame name="myframe" ... 
+0

我不能讓它工作:/這讓我感動,但在地址欄中的地址發生變化,以:'http://192.168.0.2/csharp%20projects/index.html?http :// 192.168.0.2/csharp%20projects/sitemap.html' from:'http://192.168.0.2/csha rp%20projects /' – HelpNeeder 2012-03-01 11:22:13

+0

它更改地址以告知框架集頁面應加載哪個子頁面。我認爲你不能避免地址改變。這是爲什麼許多人不喜歡框架集的限制。 – 2012-03-03 18:23:25

+1

有趣的答案:[爲什麼我不應該使用HTML框架?](http://stackoverflow.com/questions/1203068/why-should-i-not-use-html-frames);-) – 2012-03-03 18:23:53