location对象属性(一)

  1. window.location 对象用于获得当前页面的地址 (URL)
  2. window.location 对象在编写时可不使用 window 这个前缀。
  3. location 对象是 Window 对象的一个部分,可通过 window.location 属性来访问。

hash:

定义:hash 属性是一个可读可写的字符串,该字符串是 URL 的锚部分(从 # 号开始的部分)。

语法:location.hash=anchorname(锚名)

例子:

1.

function test(){

       var x = window.open("http://example.com:1234/test.htm")

       x.location.hash="#part2";

}

调用函数之后,回跳到http://example.com:1234/test.htm#part2

2.

假如当前 URL 是: http://example.com:1234/test.htm#part2

alert(location.hash);//#part2

 

host:

定义:host 属性是一个可读可写的字符串,可设置或返回当前 URL 的主机名称和端口号。

语法:location.host

例子:

假设当前的 URL 是: http://example.com:1234/test.htm#part2

alert(location.host);//example.com:1234

 

hostname:

定义:hostname 属性是一个可读可写的字符串,可设置或返回当前 URL 的主机名。

语法:location.hostname

例子:

假设当前的 URL 是: http://example.com:1234/test.htm#part2

alert(location.hostname)//example.com

 

href:

定义:href 属性是一个可读可写的字符串,可设置或返回当前显示的文档的完整 URL。因此,我们可以通过为该属性设置新的 URL,使浏览器读取并显示新的 URL 的内容。

语法:location.href=URL

例子:

假设当前的 URL 是: http://example.com:1234/test.htm#part2

alert(location.href)//http://example.com:1234/test.htm#part2

 

pathname:

定义:pathname 属性是一个可读可写的字符串,可设置或返回当前 URL 的路径部分。

语法:location.pathname=path

例子:

假设当前的 URL 是: http://example.com:1234/test/test.htm#part2

alert(location.pathname)///test/test.htm

 

port:

定义:port 属性是一个可读可写的字符串,可设置或返回当前 URL 的端口部分。

语法:location.port=portnumber

例子:

假设当前的 URL 是: http://example.com:1234/test.htm#part2

alert(location.port)//1234

 

protocol:

定义:protocol 属性是一个可读可写的字符串,可设置或返回当前 URL 的协议。

语法:location.protocol=path

例子:

假设当前的 URL 是: http://example.com:1234/test.htm#part2

alert(location.protocol)//http:

 

search:

定义:search 属性是一个可读可写的字符串,可设置或返回当前 URL 的查询部分(问号 ? 之后的部分)。

语法:location.search=path_from_questionmark

例子:

假设当前的 URL 是: http://www.w3school.com.cn/tiy/t.asp?f=hdom_loc_search

alert(location.search)//?f=hdom_loc_search

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值