在我的上一篇文章中主要介绍了window对象的常用属性和screen对象:点击打开链接点击此链接可以查看

Location对象也有一系列的属性:

hash:设置或返回从井号(#)开始的URL

host:设置或返回主机名和当前URL的端口号

hostname:设置或返回当前URL的主机名

href:设置或返回完整的URL

pathname:设置或返回当前URL的路径部分

port:设置或返回当前URL的端口号

protocol:设置或返回当前URL的协议

search:设置或返回从问号(?)开始的URL(查询部分)

Location对象的常用方法:

assign():加载新的文档

reload():重新加载当前文档

replace():用新的文档替换当前文档

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Location对象</title>
</head>
<body>
<p>一二三四五六七</p>
<img src="" alt="">
<input type="button" id="a" value="点击刷新">
</body>
<script>
    /*
    var href=location.href;
    var host=location.host;
    alert(href)
    alert(host)
    */
/*
  //改变当前页面的url地址
  location.href='https://www.baidu';
  //简便写法
    location='https://www.baidu';//页面跳转(运行时直接跳转到百度页面)
    */

window.οnlοad=function () {

    //手动刷新
    var a=document.getElementById('a');
    
    //添加点击事件
    a.οnclick=function () {
        
        //手动刷新
        location.reload()

        //修改当前文档,作用类似location
        location.replace('https://www.baidu')//点击刷新时,跳转到百度页面
        
        location.assign('https://www.baidu')//点击刷新时,跳转到百度页面
    }
}
</script>
</html>



更多推荐

HTML、js:如何利用Location对象的常用属性和方法重新加载、刷新页面