3 回答
TA貢獻1982條經(jīng)驗 獲得超2個贊
1.location 地址對象描述的是某一個窗口對象所打開的地址。表示當前窗口的地址,只需使用“l(fā)ocation”就行;若要表示某一個窗口的地址,就使用“<窗口對象>.location”。具體如下:
第一、location 屬性、用法以及相關(guān)示例:
Location包含了關(guān)于當前 URL 的信息。location 對象描述了與一個給定的 Window 對象
關(guān)聯(lián)的完整 URL。location 對象的每個屬性都描述了 URL 的不同特性。
2.屬性概覽
protocol 返回地址的協(xié)議,取值為 'http:','https:','file:' 等等。
hostname 返回地址的主機名,例如,一個“
http://www.microsoft.com/china/”的地址,location.hostname ==
'www.microsoft.com'。
· port 返回地址的端口號,一般 http 的端口號是 '80'。
· host 返回主機名和端口號,如:'www.a.com:8080'。
· pathname 返回路徑名,如“http://www.a.com/b/c.html”,
location.pathname == 'b/c.html'。
· hash 返回“#”以及以后的內(nèi)容,如“
http://www.a.com/b/c.html#chapter4”,location.hash ==
'#chapter4';如果地址里沒有“#”,則返回空字符串。
· search 返回“?”以及以后的內(nèi)容,如“
http://www.a.com/b/c.asp?selection=3&jumpto=4”,l ocation.search
== '?selection=3&jumpto=4';可以使用
“l(fā)ocation.href = '...'”,也可以直接用“l(fā)ocation = '...'”來達
到此目的。
3.方法概覽
reload() 相當于按瀏覽器上的“刷新”(IE)或“Reload”(Netscape)
鍵。
replace() 打開一個 URL,并取代歷史對象中當前位置的地址。用這個方
法打開一個 URL 后,按 下瀏覽器的“后退”鍵將不能返回到剛才的頁面。
location 之頁面跳轉(zhuǎn)js 如下:
//簡單跳轉(zhuǎn)
function gotoPage(url) {
// eg. var url =
"newsview.html?catalogid="+catalogID+"&pageid="+pageid;
window.location = url;
}
// 對location 用法的升級,為單個頁面?zhèn)鬟f參數(shù)
function goto_catalog(iCat) {
if(iCat<=0) {
top.location = "../index.aspx"; // top 出去
} else {
window.location = "../newsCat.aspx?catid="+iCat;
}
}
對指定框架進行跳轉(zhuǎn)頁面,
function goto_iframe(url) {
parent.mainFrame.location = "../index.aspx"; //
// parent.document.getElementById("mainFrame").src =
"../index.aspx";// use dom to change page // 同時我增加了dom 的寫法
}
// 對指定框架進行跳轉(zhuǎn)頁面,因為
parent.iframename.location="../index.aspx"; 方法不能實行,主要是
"parent.iframename" 中的iframename在js 中被默認為節(jié)點,而 不能把傳遞過
來的參數(shù)轉(zhuǎn)換過來,用dom 實現(xiàn)了該傳遞二個參數(shù)的框架跳轉(zhuǎn)頁面,
function goto_iframe(iframename,url) {
parent.document.getElementById(iframename).src = "../index.aspx";//
use dom to change page by iframeName
//}
// 回到首頁
function gohome() {
top.location = "/index.aspx";
TA貢獻1793條經(jīng)驗 獲得超6個贊
location對象用于訪問當前頁面的地址
屬性:
hash 設(shè)置或獲取 href 屬性中在井號“#”后面的分段。
host 設(shè)置或獲取 location 或 URL 的 hostname 和 port 號碼。
hostname 設(shè)置或獲取 location 或 URL 的主機名稱部分。
href 設(shè)置或獲取整個 URL 為字符串。
pathname 設(shè)置或獲取對象指定的文件名或路徑。
port 設(shè)置或獲取與 URL 關(guān)聯(lián)的端口號碼。
protocol 設(shè)置或獲取 URL 的協(xié)議部分。
search 設(shè)置或獲取 href 屬性中跟在問號后面的部分。
方法:
assign 裝入新的 HTML 文檔。
reload 重新裝入當前頁面。
replace 裝入指定 URL 的另外文檔來替換當前文檔。
常見用法:
location.reload(); // 刷新頁面
location = "http://www.baidu.com"; //跳轉(zhuǎn)到百度
添加回答
舉報
