JS intercepts URL parameters

Time:2021-5-9

Copy and paste the code of this page to the HTML page and open it.

Document



  
    account number: 
    password: 
    
  


  //demo.html?userName=yft&pwd=admin

  //If we can use this data in the second page, we can transfer data between two pages.

  //  {userName:yft,pwd:admin}

  //Intercept URL string
  function params(url) {
    //We just need the string after the question mark
    var arr = url.split('?')
    // console.log(arr);
    var params = arr[1];
    //console.log(params);    //userName=yft&pwd=admin
    var arr1 = params.split('&');
    console.log(arr1);
    var o = {}
    //Because there are multiple elements in Arr1, which need to be cut, we need to traverse the loop.
    for (var i = 0; i < arr1.length; i++) {
      // console.log(arr[i]);
      var newArr = arr1[i].split('=');
      console.log(newArr);
      // newArr[0]    newArr[1]
      o[newArr[0]] = newArr[1];
    }
    return o;
  }
  console.log(params('www.yft.com/login?userName=yft&pwd=admin'));