javascript中的location用法簡單介紹
更新時(shí)間:2007年03月07日 00:00:00 作者:
先前寫了一片用window.location.href實(shí)現(xiàn)刷新另個(gè)框架頁面 ,特此我看了一下locaiton的詳細(xì)用法,對此有點(diǎn)改進(jìn),現(xiàn)在我將他整理成js,方便查閱,也貼上和朋友們分享一下,具體如下:
第一、簡單介紹一下location屬性、用法以及相關(guān)示例:
Location
包含了關(guān)于當(dāng)前 URL 的信息。
描述
location 對象描述了與一個(gè)給定的 Window 對象關(guān)聯(lián)的完整 URL。location 對象的每個(gè)屬性都描述了 URL 的不同特性。
通常情況下,一個(gè) URL 會有下面的格式:
協(xié)議//主機(jī):端口/路徑名稱#哈希標(biāo)識?搜索條件 例如:
http://skylaugh.cnblogs.com/index.html#topic1?x=7&y=2 這些部分是滿足下列需求的:
“協(xié)議”是 URL 的起始部分,直到包含到第一個(gè)冒號。
“主機(jī)”描述了主機(jī)和域名,或者一個(gè)網(wǎng)絡(luò)主機(jī)的 IP 地址。
“端口”描述了服務(wù)器用于通訊的通訊端口。
路徑名稱描述了 URL 的路徑方面的信息。
“哈希標(biāo)識”描述了 URL 中的錨名稱,包括哈希掩碼(#)。此屬性只應(yīng)用于 HTTP 的 URL。
“搜索條件”描述了該 URL 中的任何查詢信息,包括問號。此屬性只應(yīng)用于 HTTP 的 URL。“搜索條件”字符串包含變量和值的配對;每對之間由一個(gè)“&”連接。
屬性概覽
hash: Specifies an anchor name in the URL.
host: Specifies the host and domain name, or IP address, of a network host.
hostname: Specifies the host:port portion of the URL.
href: Specifies the entire URL.
pathname: Specifies the URL-path portion of the URL.
port: Specifies the communications port that the server uses.
protocol: Specifies the beginning of the URL, including the colon.
search: Specifies a query.
方法概覽
reload Forces a reload of the window's current document.
replace Loads the specified URL over the current history entry.
主要功能示例,其他類同:
hash:
newWindow.location.href = http://skylaugh.cnblogs.com
newWindow.location.hash = #59831
host
A string specifying the server name, subdomain, and domain name.
newWindow.location.href = http://skylaugh.cnblogs.com
newWindow.location.host = skylaugh.cnblogs.com
href
A string specifying the entire URL.
window.location.
pathname
A string specifying the URL-path portion of the URL.
search
A string beginning with a question mark that specifies any query information in the URL.
newWindow.location.href = http://skylaugh.cnblogs.com
newWindow.location.search = ?newsid=111
二、location之頁面跳轉(zhuǎn)js如下:
//簡單跳轉(zhuǎn)
function gotoPage(url)
{
// eg. var url = "newsview.html?catalogid="+catalogID+"&pageid="+pageid;
window.location = url;
}
// 對location用法的升級,為單個(gè)頁面?zhèn)鬟f參數(shù)
function goto_catalog(iCat)
{
if(iCat<=0)
{
top.location = "../index.aspx"; // top出去
}
else
{
window.location = "../newsCat.aspx?catid="+iCat;
}
}
// 對指定框架進(jìn)行跳轉(zhuǎn)頁面,二種方法皆可用
function goto_iframe(url)
{
parent.mainFrame.location = "../index.aspx"; //
// parent.document.getElementById("mainFrame").src = "../index.aspx";// use dom to change page // 同時(shí)我增加了dom的寫法
}
// 對指定框架進(jìn)行跳轉(zhuǎn)頁面,因?yàn)?nbsp;parent.iframename.location="../index.aspx"; 方法不能實(shí)行,主要是 "parent.iframename" 中的iframename在js中被默認(rèn)為節(jié)點(diǎn),而不能把傳遞過來的參數(shù)轉(zhuǎn)換過來,所以用dom實(shí)現(xiàn)了該傳遞二個(gè)參數(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";
}
</script>
第一、簡單介紹一下location屬性、用法以及相關(guān)示例:
Location
包含了關(guān)于當(dāng)前 URL 的信息。
描述
location 對象描述了與一個(gè)給定的 Window 對象關(guān)聯(lián)的完整 URL。location 對象的每個(gè)屬性都描述了 URL 的不同特性。
通常情況下,一個(gè) URL 會有下面的格式:
協(xié)議//主機(jī):端口/路徑名稱#哈希標(biāo)識?搜索條件 例如:
http://skylaugh.cnblogs.com/index.html#topic1?x=7&y=2 這些部分是滿足下列需求的:
“協(xié)議”是 URL 的起始部分,直到包含到第一個(gè)冒號。
“主機(jī)”描述了主機(jī)和域名,或者一個(gè)網(wǎng)絡(luò)主機(jī)的 IP 地址。
“端口”描述了服務(wù)器用于通訊的通訊端口。
路徑名稱描述了 URL 的路徑方面的信息。
“哈希標(biāo)識”描述了 URL 中的錨名稱,包括哈希掩碼(#)。此屬性只應(yīng)用于 HTTP 的 URL。
“搜索條件”描述了該 URL 中的任何查詢信息,包括問號。此屬性只應(yīng)用于 HTTP 的 URL。“搜索條件”字符串包含變量和值的配對;每對之間由一個(gè)“&”連接。
屬性概覽
hash: Specifies an anchor name in the URL.
host: Specifies the host and domain name, or IP address, of a network host.
hostname: Specifies the host:port portion of the URL.
href: Specifies the entire URL.
pathname: Specifies the URL-path portion of the URL.
port: Specifies the communications port that the server uses.
protocol: Specifies the beginning of the URL, including the colon.
search: Specifies a query.
方法概覽
reload Forces a reload of the window's current document.
replace Loads the specified URL over the current history entry.
主要功能示例,其他類同:
hash:
newWindow.location.href = http://skylaugh.cnblogs.com
newWindow.location.hash = #59831
host
A string specifying the server name, subdomain, and domain name.
newWindow.location.href = http://skylaugh.cnblogs.com
newWindow.location.host = skylaugh.cnblogs.com
href
A string specifying the entire URL.
window.location.
pathname
A string specifying the URL-path portion of the URL.
search
A string beginning with a question mark that specifies any query information in the URL.
newWindow.location.href = http://skylaugh.cnblogs.com
newWindow.location.search = ?newsid=111
二、location之頁面跳轉(zhuǎn)js如下:
//簡單跳轉(zhuǎn)
function gotoPage(url)
{
// eg. var url = "newsview.html?catalogid="+catalogID+"&pageid="+pageid;
window.location = url;
}
// 對location用法的升級,為單個(gè)頁面?zhèn)鬟f參數(shù)
function goto_catalog(iCat)
{
if(iCat<=0)
{
top.location = "../index.aspx"; // top出去
}
else
{
window.location = "../newsCat.aspx?catid="+iCat;
}
}
// 對指定框架進(jìn)行跳轉(zhuǎn)頁面,二種方法皆可用
function goto_iframe(url)
{
parent.mainFrame.location = "../index.aspx"; //
// parent.document.getElementById("mainFrame").src = "../index.aspx";// use dom to change page // 同時(shí)我增加了dom的寫法
}
// 對指定框架進(jìn)行跳轉(zhuǎn)頁面,因?yàn)?nbsp;parent.iframename.location="../index.aspx"; 方法不能實(shí)行,主要是 "parent.iframename" 中的iframename在js中被默認(rèn)為節(jié)點(diǎn),而不能把傳遞過來的參數(shù)轉(zhuǎn)換過來,所以用dom實(shí)現(xiàn)了該傳遞二個(gè)參數(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";
}
</script>
您可能感興趣的文章:
- javascript 中設(shè)置window.location.href跳轉(zhuǎn)無效問題解決辦法
- javascript 打開頁面window.location和window.open的區(qū)別
- js獲取location.href的參數(shù)實(shí)例代碼
- js實(shí)現(xiàn)頁面跳轉(zhuǎn)重定向的幾種方式
- JavaScript實(shí)現(xiàn)強(qiáng)制重定向至HTTPS頁面
- js刷新頁面方法大全
- JS定時(shí)刷新頁面及跳轉(zhuǎn)頁面的方法
- 刷新頁面的幾種方法小結(jié)(JS,ASP.NET)
- js 刷新頁面的代碼小結(jié) 推薦
- JS關(guān)于刷新頁面的相關(guān)總結(jié)
- JS基于Location實(shí)現(xiàn)訪問Url、重定向及刷新頁面的方法分析
相關(guān)文章
JavaScript中Number.MIN_VALUE屬性的使用示例
這篇文章主要介紹了JavaScript中Number.MIN_VALUE屬性的使用示例,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-06-06javascript 學(xué)習(xí)筆記(八)javascript對象
昨天看了些有關(guān)javascript對象方面的文章,以下是自己的一些學(xué)習(xí)心得及體會,希望同大家共同討論!2011-04-04javascript代碼運(yùn)行不出來執(zhí)行錯(cuò)誤的可能情況整理
js代碼運(yùn)行不出來的情況在項(xiàng)目中經(jīng)常發(fā)生,究竟是什么原因呢?在本文整理了一些常見的情況,感興趣的各位朋友可以參考下2013-10-10NodeJS的url截取模塊url-extract的使用實(shí)例
本文主要介紹了NodeJS的url信息截取模塊url-extract的使用方法,最后提供了實(shí)例代碼供大家參考2013-11-11javascript數(shù)組操作總結(jié)和屬性、方法介紹
這篇文章主要介紹了javascript數(shù)組操作總結(jié),例如數(shù)組的創(chuàng)建、添加、刪除、合并等操作方法,同時(shí)總結(jié)了屬性和操作函數(shù)、方法等,需要的朋友可以參考下2014-04-04JavaScript bold方法入門實(shí)例(把指定文字顯示為粗體)
這篇文章主要介紹了JavaScript字符串對象的bold方法入門實(shí)例,bold方法用于把指定文字顯示為粗體,需要的朋友可以參考下2014-10-10