location

location对象

  • 可以通过Document.location和Window.location访问
  • 属性
    //当前URL https://developer.mozilla.org/en-US/search?q=URL#search-container
    //-----href  整个URL
    console.log(location.href);      // https://developer.mozilla.org/en-US/search?q=URL#search-container
    
    //-----protocol   URL对应协议
    console.log(location.protocol);  // https:
    
    //-----host  域名,如果端口不是:80,该串最后带有一个":"并跟上URL的端口号
    console.log(location.host);      // developer.mozilla.org
    
    //-----hostname  域名,不包含端口
    console.log(location.hostname);  // developer.mozilla.org
    
    //-----port  端口号,端口是:80,则为空字符串
    console.log(location.port);      // ''
    
    //-----pathname  URL中路径部分,开头有一个 /
    console.log(location.pathname);  // /en-US/search
    
    //-----search  URL参数,开头有一个“?”
    console.log(location.search);    // ?q=URL
    
    //-----hash  块标识符,开头有一个“#”
    console.log(location.hash);      // #search-container
    
    //-----origin  页面来源的域名的标准形式
    console.log(location.origin);    // https://developer.mozilla.org
  • 方法
    • Location.assign()

    加载给定URL的内容资源到这个Location对象所关联的对象上。

    • Location.reload()

    重新加载来自当前 URL的资源。他有一个特殊的可选参数,类型为Boolean,该参数为true时会导致该方法引发的刷新一定会从服务器上加载数据。如果是 false或没有制定这个参数,浏览器可能从缓存当中加载页面。

    • Location.replace()

    用给定的URL替换掉当前的资源。与 assign() 方法不同的是用 replace()替换的新页面不会被保存在会话的历史 History中,这意味着用户将不能用后退按钮转到该页面。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值