Window-location对象

xiaoxiao2021-02-28  71

Location 对象

包含有关当前 URL 的信息

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

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

Location 对象属性

1. hash

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

googleIEfirefoxsafarioperatruetruetruetruetrue

location.hash

document.write(location.hash);

2. host

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

googleIEfirefoxsafarioperatruetruetruetruetrue

location.host

document.write(location.host);

3. hostname

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

googleIEfirefoxsafarioperatruetruetruetruetrue

location.hostname

document.write(location.hostname);

4. href

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

googleIEfirefoxsafarioperatruetruetruetruetrue

location.href

document.write(location.href);

5. pathname

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

googleIEfirefoxsafarioperatruetruetruetruetrue

location.pathname

document.write(location.pathname );

6. port

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

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

googleIEfirefoxsafarioperatruetruetruetruetrue

location.port

document.write(location.port);

7. protocol

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

googleIEfirefoxsafarioperatruetruetruetruetrue

location.protocol

document.write(location.protocol);

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

googleIEfirefoxsafarioperatruetruetruetruetrue

location.search

document.write(location.search);

Location 对象方法

1. assign

加载一个新的文档

googleIEfirefoxsafarioperatruetruetruetruetrue

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 健的效果是完全一样。

googleIEfirefoxsafarioperatruetruetruetruetrue

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

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

googleIEfirefoxsafarioperatruetruetruetruetrue

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/

转载请注明原文地址: https://www.6miu.com/read-55797.html

最新回复(0)