簡體   English   中英

如何保存部分url中的字符串?

[英]How to save strings from part of url?

給定以下URL:

https://example.com/test/results/?usp-custom-14=2015+2017&usp-custom-8=United-States

如何保存三個變量:

var date1: "2015";
var date2: "2017";
var loc = "United States";

注意 :我們在網址2015+2017有兩個帶+日期,我們需要將它們分開。 網址中有破折號United-States ,我們需要將其作為United States

這是我正在嘗試的:

function getUrlVars() {
  var vars = [], hash;
  var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
  for(var i = 0; i < hashes.length; i++) {
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
  }
  return vars;
}
var dates = getUrlVars()["usp-custom-14"];
var loc = getUrlVars()["usp-custom-8"];
var dateSplit = dates.split("+");

另外,頁面加載后,由於其他原因,我需要再次更新URL,然后執行以下操作:

history.replaceState('data to be passed', 'Title of the page', '<?php echo getAddress(); ?>/?usp-custom-14='+dateSplit+'&usp-custom-8='+loc);

但是網址是重復的

https://example.com/test/results/?usp-custom-14=2015+2017&usp-custom-8=United-States/?usp-custom-14=2015,2017&usp-custom-8=United-States

您可以分割網址? 並使用pop()返回結果數組的最后一個成員,這將是您查詢字符串的全部。

從那里,您可以先在&上將其拆分為鍵值對,然后在=上將其拆分。

我將其放在函數中,以便您可以在需要時簡單地執行getParam("my-url-parameter") 使用它,然后處理特定參數的+- ,您應該能夠輕松獲得所需的內容。

無論何時需要,它都應該是可重用的。

 function getParam(key) { //var url = window.location.href; (Doesn't work on StackOverflow, but would be used in your real environment) var url = "https://example.com/test/results/?usp-custom-14=2015+2017&usp-custom-8=United-States"; var querystring = url.split("?").pop(); var params = {}; querystring.split("&").forEach((i) => params[i.split("=")[0]] = i.split("=")[1]); //Create key-value pairs return params[key] || null; } var uspCustom14 = getParam("usp-custom-14").split("+"); var date1 = uspCustom14[0]; var date2 = uspCustom14[1]; var country = getParam("usp-custom-8").replace(/\\-/g, ' '); console.log(`Date 1: ${date1},`, `Date 2: ${date2},`, `Country: ${country}`); 

對於第二個問題,您可以刪除查詢字符串,然后使用適當的值重新添加它:

var urlDates = getParam("usp-custom-14").replace('+',',');
var urlCountry = getParam("usp-custom-8");
history.replaceState('data to be passed', 'Title of the page', `${window.location.href.split("?")[0]}?usp-custom-14=${urlDates}&usp-custom-8=${urlCountry}`);

這應該為您提供所需的內容,同時使其盡可能接近您的原始代碼。 您可以安全地分割其中帶有“ +”的字符串。 你有“?” 和“ =”以錯誤的順序分割。

function getUrlVars() {
  var vars = [], hash;  
  var hashes = window.location.href.split('?')[1];
  var params = hashes.split('&');    
  for(var i = 0; i < params.length; i++) {  
     hash = params[i].split('=');
     vars.push(hash[0]);
     vars[hash[0]] = hash[1];    
}
return vars;
}
var dates = getUrlVars()["usp-custom-14"];
var loc = getUrlVars()["usp-custom-8"];
var dateSplit = dates.split("+");

 var str ="https://example.com/test/results/?usp-custom-14=2015+2017&usp-custom-8=United-States"; var split = str.split('usp-custom-14='); var firstDate = split[1].split('+')[0]; var secondDate = split[1].substring(split[1].lastIndexOf("+")+1,split[1].lastIndexOf('&')); var country = split[1].substring(split[1].lastIndexOf("=")+1,split[1].length-1).replace('-',' '); console.log(firstDate); console.log(secondDate); console.log(country); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM