码迷,mamicode.com
首页 > 编程语言 > 详细

javascript获取地址栏参数/值

时间:2016-04-13 18:56:50      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

//URL: http://www.example.com/?var1=val1&var2=val2=val3&test=3&test=43&aaa=#2

//window.location.search will return "?var1=val1&var2=val2=val3&test=3&test=43&aaa="
//use the location.search, because # could be remove.

//refer:
//https://gist.github.com/alkos333/1771618
//http://papermashup.com/read-url-get-variables-withjavascript/
//http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html

//1.
//getUrlVars() will return {var1: "val1", var2: "val2=val3", test: "43", aaa: ""}
function getUrlVars() {
  var vars = {};
  var parts = window.location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(match, key ,value) {
    vars[key] = value;
  });
  return vars;
}
//2.
//getUrlVars() will return array ["var1", "var2", "test", "test", "aaa"]
//getUrlvars(‘var2‘) will return "val2=val3"
function getUrlVars(key) {
  var vars = [];
  var parts = window.location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(match, key, value) {
    vars.push(key);
    vars[key] = value;
  });
  if (key) {
    return vars[key];
  }
  return vars;
}

  

javascript获取地址栏参数/值

标签:

原文地址:http://www.cnblogs.com/zlog/p/5387925.html

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