一、示例
<script>
document.write(location.host + "<br>");//返回主机名和端口号
document.write(location.hostname + "<br>");//返回主机名
document.write(location.href + "<br>");//返回完整的URL
document.write(location.pathname + "<br>");//返回URL的路径部分
document.write(location.port + "<br>");//返回端口号
document.write(location.protocol + "<br>");//返回协议
document.write(location.search + "<br>");//返回问号开始的查询部分
/*document.write(location.assign("http://www.baidu.com"));加载新的文档
document.write(location.reload(false));重新加载当前文档
参数为false ,如果服务器的文档改变,则会从服务器下载新的文档,如果服务器文档未改变,则从缓存中加载
参数为true,强制从服务器下载新的文档,跳过缓存
document.write(location.replace("http://www.baidu.com"))用新的文档替换当前文档
replace()方法不会创建新的历史记录,而是直接将之前的历史记录替换
*/
</script>
本文详细介绍了JavaScript中location对象的使用,包括获取和修改浏览器URL的各种属性和方法,如主机名、端口号、完整URL、路径、协议等。同时,还探讨了如何通过location对象的assign、reload和replace方法来实现页面跳转和刷新。
984

被折叠的 条评论
为什么被折叠?



