快速学习js笔记三 location 对象

本文详细介绍了location对象的基本属性和方法,通过实例展示了如何获取和修改URL的相关部分,包括hash、host、hostname、href、pathname、search等,并演示了跳转、加载页面、重新加载和替换页面的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        location对象被包含在window.location属性中,该对象也比较常用在开发过程中 但是也特的简单。下面以一个例子 来说明他的所有的方法跟属性

   

<html>
  <head>
   <title>location</title>
   </head>
   <body>
   <script type="text/javascript">
     document.write("hash:="+location.hash+"<br/>");                          //url中#之后的所有字符串
     document.write("host:="+location.host+"<br/>");                            //主机ip地址
     document.write("hostName:="+location.hostname+"<br/>");         //主机ip地址或者是主机的 名称
     document.write("herf:="+location.href+"<br/>");                             //当前的url
     document.write("pathName:="+location.pathname+"<br/>");         //虚拟目录路径
     document.write("search:="+location.search+"<br/>");         //?开始的字符串 要是后面 有#号就节结束,要是 ?在#后面不起作用都算为锚点。
   </script>
     <input type="button" onclick="location.href='http://baidu.com?seach#hash'" value="跳转到百度页面,并设置seach 与hash"/>
    <input type="button" onclick="location.assign('http://baidu.com?seach#hash')" value="当前页加载百度页面"/>
    <input type="button" onclick="location.reload()" value="重新加载页面"/>
    <input type="button" onclick="location.replace('http://baidu.com?seach#hash')" value="百度替换该页面"/>
    </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值