js location对象

  location提供了与当前窗口中加载文档有关的信息。还提供了一些导航功能。它既是window对象的属性,又是document对象的属性,window.location与document.location引用的是同一个对象。    

  location对象的属性,如下

示例:查询字符串参数

    //解析查询字符串,然后返回包含所有参赛的对象
    function getQueryStringArgs(){
        //取得查询字符串并去掉开头的问号
        var qs = location.search.length ? location.search.substring(1) : '',
        //保存数据的对象
        args = {},
        //取得每一项
        items = qs.length ? qs.split('&') : [],
        item = null,
        name = null,
        value = null,
        i = 0,
        len = items.length;
        //逐个将每项添加到args对象中
        for(i = 0; i <len; i++){
            item = items[i].split('=');
            name = decodeURIComponent(item[0]);
            value = decodeURIComponent(item[1]);
            if(name.length){
                args[name] = value;
            }
        }

        return args

    }
    var args = getQueryStringArgs();
    console.log(args['t']);

 

posted @ 2015-12-17 17:22  tianxintian22  阅读(471)  评论(0编辑  收藏  举报