javascript中的location用法簡單介紹
更新時間:2007年03月07日 00:00:00 作者:
先前寫了一片用window.location.href實現(xiàn)刷新另個框架頁面 ,特此我看了一下locaiton的詳細用法,對此有點改進,現(xiàn)在我將他整理成js,方便查閱,也貼上和朋友們分享一下,具體如下:
第一、簡單介紹一下location屬性、用法以及相關示例:
Location
包含了關于當前 URL 的信息。
描述
location 對象描述了與一個給定的 Window 對象關聯(lián)的完整 URL。location 對象的每個屬性都描述了 URL 的不同特性。
通常情況下,一個 URL 會有下面的格式:
協(xié)議//主機:端口/路徑名稱#哈希標識?搜索條件 例如:
http://skylaugh.cnblogs.com/index.html#topic1?x=7&y=2 這些部分是滿足下列需求的:
“協(xié)議”是 URL 的起始部分,直到包含到第一個冒號。
“主機”描述了主機和域名,或者一個網(wǎng)絡主機的 IP 地址。
“端口”描述了服務器用于通訊的通訊端口。
路徑名稱描述了 URL 的路徑方面的信息。
“哈希標識”描述了 URL 中的錨名稱,包括哈希掩碼(#)。此屬性只應用于 HTTP 的 URL。
“搜索條件”描述了該 URL 中的任何查詢信息,包括問號。此屬性只應用于 HTTP 的 URL。“搜索條件”字符串包含變量和值的配對;每對之間由一個“&”連接。
屬性概覽
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之頁面跳轉js如下:
//簡單跳轉
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;
}
}
// 對指定框架進行跳轉頁面,二種方法皆可用
function goto_iframe(url)
{
parent.mainFrame.location = "../index.aspx"; //
// parent.document.getElementById("mainFrame").src = "../index.aspx";// use dom to change page // 同時我增加了dom的寫法
}
// 對指定框架進行跳轉頁面,因為 parent.iframename.location="../index.aspx"; 方法不能實行,主要是 "parent.iframename" 中的iframename在js中被默認為節(jié)點,而不能把傳遞過來的參數(shù)轉換過來,所以用dom實現(xiàn)了該傳遞二個參數(shù)的框架跳轉頁面,希望那位仁兄不吝賜教!
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屬性、用法以及相關示例:
Location
包含了關于當前 URL 的信息。
描述
location 對象描述了與一個給定的 Window 對象關聯(lián)的完整 URL。location 對象的每個屬性都描述了 URL 的不同特性。
通常情況下,一個 URL 會有下面的格式:
協(xié)議//主機:端口/路徑名稱#哈希標識?搜索條件 例如:
http://skylaugh.cnblogs.com/index.html#topic1?x=7&y=2 這些部分是滿足下列需求的:
“協(xié)議”是 URL 的起始部分,直到包含到第一個冒號。
“主機”描述了主機和域名,或者一個網(wǎng)絡主機的 IP 地址。
“端口”描述了服務器用于通訊的通訊端口。
路徑名稱描述了 URL 的路徑方面的信息。
“哈希標識”描述了 URL 中的錨名稱,包括哈希掩碼(#)。此屬性只應用于 HTTP 的 URL。
“搜索條件”描述了該 URL 中的任何查詢信息,包括問號。此屬性只應用于 HTTP 的 URL。“搜索條件”字符串包含變量和值的配對;每對之間由一個“&”連接。
屬性概覽
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之頁面跳轉js如下:
//簡單跳轉
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;
}
}
// 對指定框架進行跳轉頁面,二種方法皆可用
function goto_iframe(url)
{
parent.mainFrame.location = "../index.aspx"; //
// parent.document.getElementById("mainFrame").src = "../index.aspx";// use dom to change page // 同時我增加了dom的寫法
}
// 對指定框架進行跳轉頁面,因為 parent.iframename.location="../index.aspx"; 方法不能實行,主要是 "parent.iframename" 中的iframename在js中被默認為節(jié)點,而不能把傳遞過來的參數(shù)轉換過來,所以用dom實現(xiàn)了該傳遞二個參數(shù)的框架跳轉頁面,希望那位仁兄不吝賜教!
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>
您可能感興趣的文章:
相關文章
js修改地址欄URL參數(shù)解決url參數(shù)問題
現(xiàn)在做網(wǎng)頁,經(jīng)常會碰到處理地址欄參數(shù)的問題,因此,就專門做了一個修改地址欄參數(shù)的方法,需要了解的朋友可以參考下2012-12-12
總結JavaScript三種數(shù)據(jù)存儲方式之間的區(qū)別
這篇文章主要介紹了JavaScript三種數(shù)據(jù)存儲方式之間的區(qū)別,指的分別是sessionStorage和localStorage以及cookie三種瀏覽器端的數(shù)據(jù)存儲方式,需要的朋友可以參考下2016-05-05
document.getElementById的簡寫方式(獲取id對象的簡略寫法)
在js編寫中,經(jīng)常需要獲取id對象,如果直接用getElementById來獲取,代碼多而且老的瀏覽器不支持這屬性,所有大家可以考慮用下面的代碼。2010-09-09
addEventListener()第三個參數(shù)useCapture (Boolean)詳細解析
true的觸發(fā)順序總是在false之前;如果多個均為true,則外層的觸發(fā)先于內層;如果多個均為false,則內層的觸發(fā)先于外層2013-11-11

