2013-10-29 58 views
0

將iframe打開到單獨的url的正確javascript語法是什麼?即我在一個帶有表單的iframe中有一個頁面,提交表單後,我想重定向到站點上的單獨頁面(同一個域),但是在父窗口而不是在iframe內部打開它。這是我已經試過:按鈕點擊跳出iframe到同一個域中的新網址點擊

protected void ReplacePageClick(object sender, EventArgs e) 
{ 
    string url = "Contact.aspx"; 

    ClientScriptManager cs = Page.ClientScript; 

    cs.RegisterClientScriptBlock(this.GetType(), "RedirectScript", "top.document.location.href = " + url, true); 
    //top.document.parent.location.href? 
    //top.document.location.replace? 
    //window.parent.location? 
    //window.top.location.href? 
    //window.top.location.replace? 
    //parent.window.location.href? 
    //top.location.href? 

    // the below works, so we know it's able to register javascript 
    //cs.RegisterClientScriptBlock(this.GetType(), "Alert", "alert('hello')", true); 
} 

回答

1

正確的語法是取決於你想要多高層次結構中去

top.location = "url" 

top.location.replace("url") 

更改頂部的父母。

我假設您使用的是IE,並且由於跨域安全阻止了對另一個域中的頁面的「訪問」,因此您的控制檯(F12)中出現「拒絕訪問」。如果在iFrame的頁面不具有相同的起源父頁面會發生這種情況

嘗試

window.open("url","_top") 

更好的解決方案是將形式靶向_top擺在首位。該瀏覽器可能會允許,因爲用戶實際上點擊某些東西來加載一個新的url地址

+0

window.open(「url」,「_ top」)工作正常,謝謝!這些頁面位於同一個域中,因此沒有訪問被拒絕的錯誤。 –

+0

然後top.location應該也工作 – mplungjan

+0

不幸的是它沒有。如果你想看一看,我可以把整個源碼放在某個地方? –