javascript中的location.href有很多種用法,主要如下。
self.location.href="/url" 當前頁面打開URL頁面
location.href="/url" 當前頁面打開URL頁面
windows.location.href="/url" 當前頁面打開URL頁面,前面三個用法相同。
this.location.href="/url" 當前頁面打開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()和都是刷新當前頁面。區(qū)別在于是否有提交數(shù)據(jù)。當有提交數(shù)據(jù)時,window.location.Reload()會提示是否提交,window.location.href=window.location.href;則是向指定的url提交數(shù)據(jù)
最重要的是window.location.href 語句可以實現(xiàn)一個框架的頁面在執(zhí)行服務器端代碼后刷新另一個框架的頁面(Response.Redirect無法達到,至少我沒有發(fā)現(xiàn)):
如:index.htm頁面中有二個框架,分別為 frameLeft和frameRight,在frameRight頁面中執(zhí)行服務器端代碼后刷新frameLeft中的頁面。
先前最常見的是注冊之后,自動刷新登陸框,讓登陸框換成已登陸頁面,只要在注冊成功的代碼之后加上一段,即可以實現(xiàn)刷新另個框架的頁面。代碼如下:
Response.Write("< script language=javascript>alert('恭喜您,注冊成功!')< /script>"); Response.Write("< script language=javascript>window.parent.frameLeft.location.href='main.html'< /script>");
這樣就搞定了ASP.NET框架頁跳轉(zhuǎn)中斷的問題。其實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頁面刷新 (當然,也可以使用子窗口的 opener 對象來獲得父窗口的對象:window.opener.document.location.reload(); )
"top.location.reload();": A頁面刷新
window.location.href和window.location區(qū)別:
當輸出 location 時,會調(diào)用 toString() 函數(shù),返回的值是 a DOMString containing the whole URL
而 location.href 表示 a DOMString containing the whole URL.
所以值是一樣的。
window.location是一個對象,包含屬性有
hash 從井號 (#) 開始的 URL(錨)
host 主機名和當前 URL 的端口號
hostname 當前 URL 的主機名
href 完整的 URL
pathname 當前 URL 的路徑部分
port 當前 URL 的端口號
protocol 當前 URL 的協(xié)議
search 從問號 (?) 開始的 URL(查詢部分)
獲取window.location.href是最常用的
window.location.Reload()和window.location.href 區(qū)別:
首先介紹兩個方法的語法:
reload 方法,該方法強迫瀏覽器刷新當前頁面。
語法:location.reload([bForceGet])參數(shù): bForceGet, 可選參數(shù), 默認為 false,從客戶端緩存里取當前頁。 true, 則以GET 方式,從服務端取最新的頁面, 相當于客戶端點擊 F5("刷新")
replace 方法,該方法通過指定URL替換當前緩存在歷史里(客戶端)的項目,因此當使用replace方法之后,你不能通過“前進”和“后退”來訪問已經(jīng)被替換的URL。
語法:location.replace(URL) 參數(shù): URL
在實際應用的時候,重新刷新頁面的時候,我們通常使用: location.reload() 或者是 history.go(0) 來做。因為這種做法就像是客戶端點F5刷新頁面,所以頁面的method="post"的時候,會出現(xiàn)“網(wǎng)頁過期”的提示。那是因為Session的安全保護機制。可以想到: 當調(diào)用 location.reload() 方法的時候, aspx頁面此時在服務端內(nèi)存里已經(jīng)存在, 因此必定是 IsPostback 的。如果有這種應用: 我們需要重新加載該頁面,也就是說我們期望頁面能夠在服務端重新被創(chuàng)建, 我們期望是 Not IsPostback 的。這里,location.replace() 就可以完成此任務。被replace的頁面每次都在服務端重新生成。你可以這么寫: location.replace(location.href)
=======================================================
<a onclick="javascript:window.location.href=window.location.href;">
<a onclick="javascript:window.location.reload();">
測試效果一樣。表單沒有提交。
<input type="submit" onclick="javascript:window.location.reload();" value="單擊" id="btnVCode" />
<input type="submit" onclick="javascript:window.location.href=window.location.href;" value="單擊" id="btnVCode" />
都提交數(shù)據(jù)
window.location.Reload()應該是刷新.(如果有數(shù)據(jù)提交的話,會提示是否提交的(是和否選項))
window.location.href=window.location.href; 是定向url提交數(shù)據(jù)
最好不要用location.reload(),而用location=location比較好,還有在模式窗口(showModalDialog和showModelessDialog)前者不能用。
reload參數(shù)有true和false,比較有意思?
避免重復提交:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string nr = ((DataRowView)e.Item.DataItem).Row["GZZDS"].ToString();
string id = ((DataRowView)e.Item.DataItem).Row["RZID"].ToString();
string rid = ((DataRowView)e.Item.DataItem).Row["RWXH"].ToString();
string link = "";
if (nr == "")
{
link = "<a href='#' onclick=\"selectGuide2('BookDoc.aspx?type=2&Id=" + id + "&rid=" + rid + "&Rnd='+Math.random());location=location;\">選擇指導書</a>";
}
else
{
string t = (ViewState["State"].ToString() == "Query" || ((DataRowView)e.Item.DataItem).Row["GZZT"].ToString() == "工作結(jié)束") ? "false" : "true";
string path=((DataRowView)e.Item.DataItem).Row["GZZDSPath"].ToString();
link = "<a href='#' onclick=\"EditWord('" + path + "'," + t + ")\">" + nr + "</a>";
}
((Literal)e.Item.FindControl("Literal1")).Text = link;
}
}
window.location.Reload()和window.location.href window.location.Reload()應該是刷新.(如果有數(shù)據(jù)提交的話,會提示是否提交的(是和否選項))
window.location.href=window.location.href;
是定向url提交數(shù)據(jù)
是大的區(qū)別還是是否提交數(shù)據(jù)了
function refresh()
{
//刷新頁面函數(shù)
//window.focus();刷新窗口
//document.execCommand("Refresh");刷新窗口
//self.location.reload();刷新當前窗口
parent.location.reload();刷新父窗口
//aaa.location.reload();彈出窗口刷新父窗口
}
使用window.location.replace() or window.location.href(), 來重新加載此頁面不出現(xiàn)提示框
如對本文有疑問,請?zhí)峤坏浇涣髡搲瑥V大熱心網(wǎng)友會為你解答??! 點擊進入論壇