Window-location对象

6 篇文章 0 订阅
4 篇文章 0 订阅

Location 对象

包含有关当前 URL 的信息

Location 对象是 window 对象的一部分,可通过 window.Location 属性对其进行访问。

注意: 没有应用于Location对象的公开标准,不过所有浏览器都支持该对象。

Location 对象属性

1. hash

一个可读可写的字符串,该字符串是 URL 的锚部分(从 # 号开始的部分)

googleIEfirefoxsafariopera
truetruetruetruetrue

location.hash

document.write(location.hash);

2. host

可读可写的字符串,可设置或返回当前 URL 的主机名称和端口号

googleIEfirefoxsafariopera
truetruetruetruetrue

location.host

document.write(location.host);

3. hostname

可读可写的字符串,可设置或返回当前 URL 的主机名

googleIEfirefoxsafariopera
truetruetruetruetrue

location.hostname

document.write(location.hostname);

4. href

可读可写的字符串,可设置或返回当前显示的文档的完整 URL

googleIEfirefoxsafariopera
truetruetruetruetrue

location.href

document.write(location.href);

5. pathname

可读可写的字符串,可设置或返回当前 URL 的路径部分

googleIEfirefoxsafariopera
truetruetruetruetrue

location.pathname

document.write(location.pathname );

6. port

可读可写的字符串,可设置或返回当前 URL 的端口部分

注意:如果端口号就是80(这是默认的端口号),无需指定。
googleIEfirefoxsafariopera
truetruetruetruetrue

location.port

document.write(location.port);

7. protocol

可读可写的字符串,可设置或返回当前 URL 的协议

googleIEfirefoxsafariopera
truetruetruetruetrue

location.protocol

document.write(location.protocol);

可读可写的字符串,可设置或返回当前 URL 的查询部分(问号 ? 之后的部分)

googleIEfirefoxsafariopera
truetruetruetruetrue

location.search

document.write(location.search);

Location 对象方法

1. assign

加载一个新的文档

googleIEfirefoxsafariopera
truetruetruetruetrue

location.assign(URL)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
<script>
function newDoc(){
    window.location.assign("http://www.zshgrwz.cn/360")
}
</script>
</head>
<body>

<input type="button" value="载入新文档" onclick="newDoc()">

</body>
</html>

2. reload

用于刷新当前文档

reload() 方法类似于你浏览器上的刷新页面按钮。

如果把该方法的参数设置为 true,那么无论文档的最后修改日期是什么,它都会绕过缓存,从服务器上重新下载该文档。这与用户在单击浏览器的刷新按钮时按住 Shift 健的效果是完全一样。

googleIEfirefoxsafariopera
truetruetruetruetrue

location.reload(Boolean)

参数
  • 可选
    • Boolean 如果把该方法的参数设置为 true,那么无论文档的最后修改日期是什么,它都会绕过缓存,从服务器上重新下载该文档。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
<script>
function newDoc(){
    location.reload();
}
</script>
</head>
<body>

<input type="button" value="载入新文档" onclick="newDoc()">

</body>
</html>

3. replace

可用一个新文档取代当前文档

googleIEfirefoxsafariopera
truetruetruetruetrue

location.replace(newURL)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
<script>
function replaceDoc(){
    window.location.replace("http://www.runoob.com")
}
</script>
</head>
<body>

<input type="button" value="载入新文档替换当前页面" onclick="replaceDoc()">

</body>
</html>

文档内容出自 W3cSchool和菜鸟教程,
如需查看更详细的有关内容 请登录 http://www.w3school.com.cn/http://www.runoob.com/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值