location对象

location对象属性

  • hash    返回#后跟0或者多个字符串,没有#或者#后没有字符则返回空字符串   '#page=2'
  • host    返回服务器名称和端口号   "www.createhy.com:8888"
  • hostname    返回服务器名称   "www.createhy.com"
  • href   返回完整的url    "http://www.createhy.com:8888/CR8000Web/",location.toString()也返回此值。
  • pathname    返回url中的目录和文件名   "/CR8000Web/"
  • port    返回端口号,如果不含,则返回空   "8888"
  • protocol    返回协议 ,一般是http或者https    "http:"
  • search    返回地址栏中'?'及后续字符串(查询字符串)   "?dd=ee"

查询字符串函数:

function setQueryStringArgs() {
    var qs = location.search.length > 0?location.search.substring(1):'',
      args = {}, // 数据对象
      name = null,
      value = null,
      items = [], // 存储数组
      i = 0,
      item = qs.length ? qs.split('&'):[],
      len = item.length;

    for (i;i<len;i++) {
      items = item[i].split('=');
      name = decodeURIComponent(items[0]);
      value = decodeURIComponent(items[1]);

      if (name.length) {
        args[name] = value;
      }
    }

    return args;
  }

位置操作

location(除hash属性外),设置新值,都会导致浏览器位置改变,增加一条历史纪录
除此之外:

  • location.replace('url'), //不会创建新的历史纪录
  • location.assign(url), // 显示打开新的url,并且创建一条新的纪录
  • location.reload(), // 重新加载(有可能冲缓存中加载)
  • location.reload(true), // 重新加载(从服务器重新加载)

注意:reload()最好放在最后,因为reload()之后的代码,受到网络延迟或者系统资源的影响,可能不会执行。

posted @ 2018-06-26 14:20  转角90  阅读(245)  评论(0)    收藏  举报