javascript中的location.href有很多種用法,主要如下。
self.location.href="/url" 當(dāng)前頁面打開URL頁面
location.href="/url" 當(dāng)前頁面打開URL頁面
windows.location.href="/url" 當(dāng)前頁面打開URL頁面,前面三個(gè)用法相同。
this.location.href="/url" 當(dāng)前頁面打開URL頁面
parent.location.href="/url" 在父頁面打開新頁面
top.location.href="/url" 在頂層頁面打開新頁面
如果頁面中自定義了frame,那么可將parent self top換為自定義frame的名稱,效果是在frame窗口打開url地址
此外,window.location.href=window.location.href;和window.location.Reload()和都是刷新當(dāng)前頁面。區(qū)別在于是否有提交數(shù)據(jù)。當(dāng)有提交數(shù)據(jù)時(shí),window.location.Reload()會提示是否提交,window.location.href=window.location.href;則是向指定的url提交數(shù)據(jù)
在寫ASP.Net程序的時(shí)候,我們經(jīng)常遇到跳轉(zhuǎn)頁面的問題,我們經(jīng)常使用Response.Redirect 做ASP.NET框架頁跳轉(zhuǎn),如果客戶要在跳轉(zhuǎn)的時(shí)候使用提示,這個(gè)就不靈光了,如:
代碼如下:
Response.Write("< script>alert('恭喜您,注冊成功!');< /script>");
Response.Redirect("main.html");
這時(shí)候我們的提示內(nèi)容沒有出來就跳轉(zhuǎn)了,和Response.Redirect("main.html");沒有任何區(qū)別。
這時(shí)我們采用下面代碼試驗(yàn)一下:
ASP.NET框架頁跳轉(zhuǎn)的另一實(shí)現(xiàn)
代碼如下:
Response.Write("< script language=javascript>alert('恭喜您,注冊成功!')< /script>");
Response.Write("< script language=javascript>window.location.href='main.html'< /script>");
這個(gè)即實(shí)現(xiàn)了我們的要求,在提示后,跳轉(zhuǎn)頁面。
最重要的是window.location.href 語句可以實(shí)現(xiàn)一個(gè)框架的頁面在執(zhí)行服務(wù)器端代碼后刷新另一個(gè)框架的頁面(Response.Redirect無法達(dá)到,至少我沒有發(fā)現(xiàn)):
如:index.htm頁面中有二個(gè)框架,分別為 frameLeft和frameRight,在frameRight頁面中執(zhí)行服務(wù)器端代碼后刷新frameLeft中的頁面。
先前最常見的是注冊之后,自動刷新登陸框,讓登陸框換成已登陸頁面,只要在注冊成功的代碼之后加上一段,即可以實(shí)現(xiàn)刷新另個(gè)框架的頁面。
代碼如下:
Response.Write("< script language=javascript>alert('恭喜您,注冊成功!')< /script>");
Response.Write("< script language=javascript>window.parent.frameLeft.location.href='main.html'< /script>");
這樣就搞定了ASP.NET框架頁跳轉(zhuǎn)中斷的問題。其實(shí)asp、php中一般都使用這種方式。
"window.location.href"、"location.href"是本頁面跳轉(zhuǎn)
"parent.location.href"是上一層頁面跳轉(zhuǎn)
"top.location.href"是最外層的頁面跳轉(zhuǎn)
舉例說明:
如果A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,如果D中js這樣寫
"window.location.href"、"location.href":D頁面跳轉(zhuǎn)
"parent.location.href":C頁面跳轉(zhuǎn)
"top.location.href":A頁面跳轉(zhuǎn)
如果D頁面中有form的話,
<form>: form提交后D頁面跳轉(zhuǎn)
<form target="_blank">: form提交后彈出新頁面
<form target="_parent">: form提交后C頁面跳轉(zhuǎn)
<form target="_top"> : form提交后A頁面跳轉(zhuǎn)
關(guān)于頁面刷新,D 頁面中這樣寫:
"parent.location.reload();": C頁面刷新 (當(dāng)然,也可以使用子窗口的 opener 對象來獲得父窗口的對象:window.opener.document.location.reload(); )
"top.location.reload();": A頁面刷新
更多信息請查看IT技術(shù)專欄