用js 获取 url?x='a' 参数信息

js没有自己可以写个:
  获取URL参数
   function Request(strName)
  {
    var strHref =document.location.href;
    var intPos = strHref.indexOf("?");
    var strRight = strHref.substr(intPos + 1);
    var arrTmp = strRight.split("&");
    for(var i = 0; i < arrTmp.length; i++)
    {
      var arrTemp = arrTmp[i].split("=");
      if(arrTemp[0].toUpperCase() == strName.toUpperCase())
         return arrTemp[1];
    }
    return "";
   }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: To get the value of the `id` parameter from the URL `https://www.baidu.com/userphoto.html?id=3192060242672264723=1` using JavaScript, you can use the `URLSearchParams` API as follows: ``` const urlParams = new URLSearchParams(window.location.search); const id = urlParams.get('id'); ``` This will extract the value of the `id` parameter from the URL, which in this case is `3192060242672264723`. You can then use any method to pass this value to the backend. This could involve making an AJAX request or submitting a form with the `id` value as a parameter. Here is an example of how you could use AJAX to send the `id` value to a backend endpoint: ``` const xhttp = new XMLHttpRequest(); xhttp.open("POST", "/backend", true); xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhttp.onreadystatechange = function() { if (this.readyState === 4 && this.status === 200) { console.log(this.responseText); } }; xhttp.send("id=" + id); ``` This sends a POST request to the `/backend` endpoint with the `id` value as a parameter. Note that you would need to replace `/backend` with the actual URL of your backend endpoint. ### 回答2: 在JavaScript中,我们可以使用`location.search`来获取URL中问号后面的参数部分。对于给定的URL https://www.baidu.com/userphoto.html?id=3192060242672264723=1,我们可以使用以下代码来获取id的值: ```javascript var urlParam = location.search.substr(1); // 获取问号后面的参数部分,去除问号 var paramArr = urlParam.split('&'); // 将参数分割成数组 var idValue = ''; // 用于存储id的值 // 遍历参数数组,查找名为"id"的参数,并获取其值 for (var i = 0; i < paramArr.length; i++) { var pair = paramArr[i].split('='); // 将参数拆分成键值对 if (pair[0] === 'id') { // 如果参数名等于"id" idValue = pair[1]; // 获取该参数的值 break; // 找到后跳出循环 } } // 将获取到的id值传给后端,这里以发送Ajax请求为例 // 假设后端接口为https://www.example.com/api,接受id作为参数 var requestUrl = 'https://www.example.com/api?id=' + idValue; // 发送Ajax请求 // ... ``` 通过上述代码,我们可以提取URL中的id值,并将其传递给后端。具体的后端处理逻辑会根据不同的业务需求而有所不同。 ### 回答3: 在JavaScript中,可以通过以下代码获取URL中的id的值,并将其传递给后端: ```javascript // 获取URL中的完整地址 var url = window.location.href; // 利用正则表达式获取id的值 var reg = /id=([^&]+)/; var id = reg.exec(url)[1]; // 将id的值传递给后端 // 你可以使用Ajax或者form表单提交等方式将id传递给后端。以下是使用Ajax的示例: var xhr = new XMLHttpRequest(); xhr.open("POST", "http://后端地址", true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { // 请求成功的处理 } }; xhr.send("id=" + id); ``` 上述代码首先通过`window.location.href`获取当前页面的URL,然后使用正则表达式提取URL中的id的值。接下来,可以通过Ajax将id的值传递给后端。在这个示例中,使用POST方法将id作为参数发送给后端,你需要将"http://后端地址"替换为你的后端接口地址。 当然,上述仅是一个示例,根据具体情况你可能需要根据后端接口的要求进行相应的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值