欢迎来到福编程网,本站提供各种互联网专业知识!

javascript中的location用法简单介绍

发布时间:2007-03-07 作者: 来源:转载
先前写了一片用window.location.href实现刷新另个框架页面,特此我看了一下locaiton的详细用法,对此有点改进,现在我将他整理成js,方便查阅,也贴上和朋友们分享一下,具体如下:第一、简单介绍一下location属性、用法以及相关示例:Location包含了关于当前
先前写了一片用window.location.href实现刷新另个框架页面,特此我看了一下locaiton的详细用法,对此有点改进,现在我将他整理成js,方便查阅,也贴上和朋友们分享一下,具体如下:

第一、简单介绍一下location属性、用法以及相关示例:
Location
包含了关于当前URL的信息。

描述
location对象描述了与一个给定的Window对象关联的完整URL。location对象的每个属性都描述了URL的不同特性。
通常情况下,一个URL会有下面的格式:

协议//主机:端口/路径名称#哈希标识?搜索条件例如:

http://skylaugh.cnblogs.com/index.html#topic1?x=7&y=2这些部分是满足下列需求的:

“协议”是URL的起始部分,直到包含到第一个冒号。
“主机”描述了主机和域名,或者一个网络主机的IP地址。
“端口”描述了服务器用于通讯的通讯端口。
路径名称描述了URL的路径方面的信息。
“哈希标识”描述了URL中的锚名称,包括哈希掩码(#)。此属性只应用于HTTP的URL。
“搜索条件”描述了该URL中的任何查询信息,包括问号。此属性只应用于HTTP的URL。“搜索条件”字符串包含变量和值的配对;每对之间由一个“&”连接。

属性概览
hash:SpecifiesananchornameintheURL.
host:Specifiesthehostanddomainname,orIPaddress,ofanetworkhost.
hostname:Specifiesthehost:portportionoftheURL.
href:SpecifiestheentireURL.
pathname:SpecifiestheURL-pathportionoftheURL.
port:Specifiesthecommunicationsportthattheserveruses.
protocol:SpecifiesthebeginningoftheURL,includingthecolon.
search:Specifiesaquery.

方法概览
reloadForcesareloadofthewindow'scurrentdocument.
replaceLoadsthespecifiedURLoverthecurrenthistoryentry.


主要功能示例,其他类同:
hash:

newWindow.location.href=http://skylaugh.cnblogs.com
newWindow.location.hash=#59831


host
Astringspecifyingtheservername,subdomain,anddomainname.
newWindow.location.href=http://skylaugh.cnblogs.com
newWindow.location.host=skylaugh.cnblogs.com


href
AstringspecifyingtheentireURL.

window.location.href="http://home.netscape.com/"


pathname
AstringspecifyingtheURL-pathportionoftheURL.

search
AstringbeginningwithaquestionmarkthatspecifiesanyqueryinformationintheURL.

newWindow.location.href=http://skylaugh.cnblogs.com
newWindow.location.search=?newsid=111

二、location之页面跳转js如下:
//简单跳转
functiongotoPage(url)
{
//eg.varurl="newsview.html?catalogid="+catalogID+"&pageid="+pageid;
window.location=url;
}
//对location用法的升级,为单个页面传递参数
functiongoto_catalog(iCat)
{
if(iCat<=0)
{
top.location="../index.aspx";//top出去
}
else
{
window.location="../newsCat.aspx?catid="+iCat;
}
}
//对指定框架进行跳转页面,二种方法皆可用
functiongoto_iframe(url)
{
parent.mainFrame.location="../index.aspx";//
//parent.document.getElementById("mainFrame").src="../index.aspx";//usedomtochangepage//同时我增加了dom的写法
}
//对指定框架进行跳转页面,因为parent.iframename.location="../index.aspx";方法不能实行,主要是"parent.iframename"中的iframename在js中被默认为节点,而不能把传递过来的参数转换过来,所以用dom实现了该传递二个参数的框架跳转页面,希望那位仁兄不吝赐教!
functiongoto_iframe(iframename,url)
{
parent.document.getElementById(iframename).src="../index.aspx";//usedomtochangepagebyiframeName

//}
//回到首页
functiongohome()
{
top.location="/index.aspx";
}

相关推荐