码迷,mamicode.com
首页 > 其他好文 > 详细

地址栏参数获取

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

标签:获得   this   object   地址   string   null   原理   name   .com   

两种方法获取地址栏中传递的参数
第一种:字符串拆分法 window.location.href 或者 location.href 或者 window.location 获得地址栏中的所有内容
decodeURI()可以解码地址栏中的数据 恢复中文数据
window.search 获得地址栏中问号及问号之后的数据 //获取地址栏里(URL)传递的参数 function GetRequest(value) { //url例子:www.bicycle.com?id="123456"&Name="bicycle"; var url = decodeURI(location.search); //?id="123456"&Name="bicycle";
    var object = {};
    if(url.indexOf("?") != -1)//url中存在问号,也就说有参数。  
    {   
      var str = url.substr(1);  //得到?后面的字符串
      var strs = str.split("&");  //将得到的参数分隔成数组[id="123456",Name="bicycle"];
      for(var i = 0; i < strs.length; i ++)  
        {   
        object[strs[i].split("=")[0]]=strs[i].split("=")[1]
      }
  }
    return object[value];  
}  
第二种:正则匹配法 这种方法其实原理和上一种方法类似,都是从URL中提取,只是提取的方法不同而已。 function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) { return unescape(r[2]); } return null; }

在vue中可以通过this.$route获取路由对象然后根据具体需要取对象内容
this.$route.path 当前页面路由
this.$route.params 路由参数
this.$route.query 查询路由参数

地址栏参数获取

标签:获得   this   object   地址   string   null   原理   name   .com   

原文地址:https://www.cnblogs.com/byksj/p/9971725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!