URL拿值

  1. ;(function(win, undefined){
  2. const URL_TOOL = {
  3. //获取url中的所有key,value值,返回对象
  4. getUrlKeyVal(URL) {
  5. let _options = {};
  6. //或者 let urlStr = URL.match((/\?([\w\W]*)/) || [, '/'])[1];
  7. let urlStr = URL.match(/\?.*/)[0].slice(1);
  8. urlStr.split('&').forEach(function(item, i){
  9. _options[item.split('=')[0]] = item.split('=')[1];
  10. });
  11. return _options;
  12. }
  13. //--------------拿url中的某一个键的值,返回字符串
  14. getUrlVal(URL, key) {
  15. var keyReg = new RegExp(key + "=([^&]+)");
  16. var val = (URL.match(keyReg) || [, '/'])[1];
  17. return val;
  18. }
  19. };
  20. window.URL_TOOL = URL_TOOL;
  21. })(window);
  22. let SearchURL = "http://www.baidu.com/?appid=wxd9j1ad5f&body=test&device_info=1000&wode=&mch_id=10000100&nonce_str=ibuaiVxkhJA";
  23. console.log(URL_TOOL.getUrlKeyVal(SearchURL));
  24. console.log(URL_TOOL.getUrlVal(SearchURL, "appid"));
  1. // 获取url所带参数
  2. function GetRequest() {
  3. var url = location.search; //获取url中"?"符后的字串
  4. var theRequest = new Object();
  5. if (url.indexOf("?") != -1) {
  6. var str = url.substr(1);
  7. strs = str.split("&");
  8. for(var i = 0; i < strs.length; i ++) {
  9. theRequest[strs[i].split("=")[0]]=(strs[i].split("=")[1]);
  10. }
  11. }
  12. return theRequest;
  13. }
  14. // 获取url特定key val
  15. function GetQueryString(name) {
  16. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
  17. var r = window.location.search.substr(1).match(reg);
  18. if (r!=null) return (r[2]); return null;
  19. }