首页 > 其他 > 详细

location对象

时间:2018-09-08 22:11:11      阅读:174      评论:0      收藏:0      [点我收藏+]

location对象是很特别的一个对象,因为它既是window对象的属性,也是document对象的属性。换句话说,window.location和document.location引用的是同一个对象。这里我们就简单的介绍一下location处理URL片段的方法吧!

一、

 如下每个属性前省去location前缀:

  hash    ‘#xxxxx‘         返回URL中的hash(即#号后面的字符)

  host     ‘www.baidu.com:80‘    返回服务器名称和端口号

  hostname  ‘www.baidu.com‘     返回不带端口号的服务器名称

  href     ‘http:www.baidu.com‘   返回当前加载页面的完整URL,而location对象的toString()方法也返回这个值。

  path     ‘8080‘         返回当前URL的指定的端口号

  pathname   ‘/xxxx/sssss/‘      返回URL中的目录和(或)文件名

  portocol(协议) ‘http:‘         返回页面使用的协议  http: 或 https: 

  search    ‘?q=test01‘       返回URL的查询字符串。 这个字符串以问号开头

二、

URL查询字符串参数:

  1.获取search

  function getQueryStringArgs () {

    // 获取查询字符并去掉开头的问号

    var searchStr = location.search.length > 0 ? location.search.substring(1) : ‘‘,

     // 保存数据的对象

     args = {},

    // 获取每一项参数

    tems = searchStr.length ? searchStr.split(‘&‘) : [],

    item = null,

    name = null,

    value = null,

    i = 0,

    len = items.length;

    // 逐个将每一项添加到args对象中

    for (i = 0; i < len; i++) {

      item = items[i].split(‘=‘);

      name = decodeURLComponent(item[0]);

      value = decodeURLComponent(item[1]);

     if (name.length) {

      args[name] = value

    }

   }

  }

location对象

原文:https://www.cnblogs.com/qinwenlong/p/9610367.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!