现在的位置: 首页 > 综合 > 正文

Javascript脚本:Location对象

2013年02月18日 ⁄ 综合 ⁄ 共 890字 ⁄ 字号 评论关闭

 Location 对象是 Window 对象的一个部分,可通过 window.location 属性来访问。
举例说下 Location 对象 的属性与方法

hash:设置或返回从井号 (#) 开始的 URL(锚)
网址:http://www.csdn.net/index.html#head
返回:#head
<script type="text/javascript">
document.write(location.pathname);
</script>

host:设置或返回主机名和当前 URL 的端口号
网址:http://www.csdn.net:3389/index.html
返回:www.csdn.net:3389

hostname:设置或返回当前 URL 的主机名
网址:http://www.csdn.net:3389/index.html
返回:www.csdn.net

href:设置或返回完整的 URL
网址:http://www.csdn.net:3389/index.html
返回:http://www.csdn.net:3389/index.html

pathname:设置或返回当前 URL 的路径部分
网址:http://www.csdn.net/index.html#head
返回:/index.html

port:设置或返回当前 URL 的端口号
网址:http://www.csdn.net:3389/index.html
返回:3389

protocol:设置或返回当前 URL 的协议
网址:http://www.csdn.net:3389/index.html
返回:http:

search:设置或返回从问号 (?) 开始的 URL(查询部分)。
网址:http://www.csdn.net/index.html?id=333
返回:?id=333

assign():加载新的文档
语法:window.location.assign(URL)

reload():重新加载当前文档
语法:window.location.reload()

replace():用新的文档替换当前文档
语法:window.location.replace()

【上篇】
【下篇】

抱歉!评论已关闭.