1.只能包含?号后面的参数

  1. var str2='https://www.baidu.com/s?wd=JS&rsv_spt=1&a=1&b=2';
  2. str2=str2.split('?')[1].split('&');
  3. var obj={};
  4. str2.forEach((item)=>{
  5. var inner=item.split('=');
  6. obj[inner[0]]=inner[1];
  7. });
  1. var str2='https://www.baidu.com/s?wd=JS&rsv_spt=1&a=1&b=2';
  2. var res3=str2.split('?');
  3. var ary=res3[1].split('&');
  4. var obj={};
  5. //item是数组中的每一项 index是该项索引
  6. //forEach是数组的方法,只能在数组中用
  7. ary.forEach((item,index)=>{
  8. var inner=item.split('=');
  9. var shuxingming=inner[0];
  10. var shuxingzhi=inner[1];
  11. obj[shuxingming]=shuxingzhi;
  12. })

2.可以获取?urlencoded 值和哈希值

url?的值 叫做 urlencoded #号后边的值叫做 哈希值

  1. function queryURLParsms(url) {
  2. let askIndex = url.indexOf('?'),
  3. poIndex = url.lastIndexOf('#'),
  4. poText = '',
  5. askText = '';
  6. //如果poIndex不存在
  7. poIndex === -1?poIndex = url.length:null;
  8. poText = url.substring(poIndex + 1);
  9. if (askIndex > -1) {
  10. askText = url.substring(askIndex + 1, poIndex);
  11. }
  12. var obj = {};
  13. if (askText) {
  14. askText.split("&").forEach(item => {
  15. var arr = item.split("=")
  16. obj[arr[0]] = arr[1];
  17. });
  18. }
  19. if (poText.length > 0) {
  20. obj["_HASH"] = poText;
  21. }
  22. return obj
  23. };
  24. let url = "http://www.zhufengpeixun.cn/?lx=1&from=wx#video";
  25. let ur2 = "http://www.zhufengpeixun.cn/";

3.内置类 利用a标签的herf属性

  1. String.prototype.queryURLParams = function queryURLParams(parms) {
  2. let link = document.createElement('a');
  3. link.href = this;
  4. let askText = link.search.substring(1),
  5. poText = link.hash.substring(1),
  6. obj = {};
  7. poText ? obj['_HASH'] = poText : null;
  8. if (askText) {
  9. let arr = askText.split(/(?:&|=)/g);
  10. for (let i = 0; i < arr.length; i += 2) {
  11. obj[arr[i]] = arr[i + 1];
  12. }
  13. }
  14. return parms?obj[parms]:'';
  15. }
  16. let url = "http://www.zhufengpeixun.cn/?lx=1&from=wx#video";
  17. console.log(url.queryURLParams("from")); //=>"wx"
  18. console.log(url.queryURLParams("_HASH")); //=>"video"

4.利用正则

  1. String.prototype.queryURLParams = function queryURLParams(parms) {
  2. let link = document.createElement('a');
  3. link.href = this;
  4. let askText = link.search.substring(1),
  5. poText = link.hash.substring(1),
  6. obj = {};
  7. poText ? obj['_HASH'] = poText : null;
  8. if (askText) {
  9. let arr = askText.split(/(?:&|=)/g);
  10. for (let i = 0; i < arr.length; i += 2) {
  11. obj[arr[i]] = arr[i + 1];
  12. }
  13. }
  14. return parms?obj[parms]:'';
  15. }
  16. let url = "http://www.zhufengpeixun.cn/?lx=1&from=wx#video";
  17. console.log(url.queryURLParams("from")); //=>"wx"
  18. console.log(url.queryURLParams("_HASH")); //=>"video"
String.prototype.queryURLParams = function queryURLParams() {
    // this->url
    let obj = {};
    this.replace(/#([^?&#=]+)/g, (_, $1) => obj['HASH'] = $1);
    this.replace(/([^?&#=]+)=([^?&#=]+)/g, (_, $1, $2) => obj[$1] = $2);
    return obj;
};
let url = 'http://www.zhufengpeixun.cn/index.html?name=xxx&age=12#teacher';
console.log(url.queryURLParams());
// {name:'xxx',age:'12',HASH:'teacher'}