javascript中的location用法簡(jiǎn)單介紹
2024-09-06 12:43:13
供稿:網(wǎng)友
先前寫了一片用window.location.href實(shí)現(xiàn)刷新另個(gè)框架頁(yè)面 ,特此我看了一下locaiton的詳細(xì)用法,對(duì)此有點(diǎn)改進(jìn),現(xiàn)在我將他整理成js,方便查閱,也貼上和朋友們分享一下,具體如下:
第一、簡(jiǎn)單介紹一下location屬性、用法以及相關(guān)示例:
Location
包含了關(guān)于當(dāng)前 URL 的信息。
描述
location 對(duì)象描述了與一個(gè)給定的 Window 對(duì)象關(guān)聯(lián)的完整 URL。location 對(duì)象的每個(gè)屬性都描述了 URL 的不同特性。
通常情況下,一個(gè) URL 會(huì)有下面的格式:
協(xié)議//主機(jī):端口/路徑名稱#哈希標(biāo)識(shí)?搜索條件 例如:
http://skylaugh.cnblogs.com/index.html#topic1?x=7&y=2 這些部分是滿足下列需求的:
“協(xié)議”是 URL 的起始部分,直到包含到第一個(gè)冒號(hào)。
“主機(jī)”描述了主機(jī)和域名,或者一個(gè)網(wǎng)絡(luò)主機(jī)的 IP 地址。
“端口”描述了服務(wù)器用于通訊的通訊端口。
路徑名稱描述了 URL 的路徑方面的信息。
“哈希標(biāo)識(shí)”描述了 URL 中的錨名稱,包括哈希掩碼(#)。此屬性只應(yīng)用于 HTTP 的 URL。
“搜索條件”描述了該 URL 中的任何查詢信息,包括問(wèn)號(hào)。此屬性只應(yīng)用于 HTTP 的 URL?!八阉鳁l件”字符串包含變量和值的配對(duì);每對(duì)之間由一個(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