【问题标题】:How to save strings from part of url?如何保存部分网址中的字符串?
【发布时间】:2018-06-06 02:44:52
【问题描述】:

给定以下网址:

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";

注意:我们在 url 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);

但是url是重复的

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

【问题讨论】:

  • @Eddie 这不是真正的重复,我已经提供了我对解决方案的看法,并且根据保存变量和更新 url,我也有不同的需求
  • 如果您正在寻找对现有解决方案的改进,请查看codereview.stackexchange.com
  • @rob.m 如果人们对您要说的内容感到困惑,请考虑澄清问题。
  • 可能第一个重复来自&lt;?php echo getAddress(); ?&gt;

标签: javascript jquery


【解决方案1】:

您可以在? 上拆分网址并使用pop() 返回结果数组的最后一个成员,这将是您的整个查询字符串。

从那里,您可以将其拆分为键值对,首先将其拆分为 &amp;,然后再拆分为 =

我把它放在一个函数中,这样你就可以在需要时简单地执行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}`);

【讨论】:

  • @rob.m 如 cmets 所示,请务必删除我的 var url 行,并将其替换为我已注释掉的行。在您的版本中,您需要使用 window.location.href,但这将在 StackOverflow 上工作。
  • 是的,当然,现在尝试,只要记住根据问题包含最后一位 history.replaceState('data to be passed', 'Title of the page', url + '?usp-custom-14='+date1 date2+ +'&amp;usp-custom-8='+country);,由于其他原因,我需要再次替换 url
  • 您的重复问题不是由您问题中的任何代码引起的。它可能在您的 PHP getAddress() 函数中。
  • @是的,我知道,删除了那个 php 位。但我仍然需要重新填充它..
【解决方案2】:

这应该会给你你想要的东西,同时尽可能地接近你的原始代码。您可以安全地拆分带有“+”的字符串。你有“?”和 "=" 以错误的顺序拆分。

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("+");

【讨论】:

    【解决方案3】:

    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);

    【讨论】:

    • 嗯。这个答案 (A) 提供 zero 上下文 (B) 删除国家的最后一个字母 (C) 将如果查询参数的顺序不是这个确切的情况,则中断。
    • @TylerRoper 上下文我理解它,因为我可以阅读它的作用,你对最后一个字母是对的,但没有注意到它。你能解决它吗?
    • @TylerRoper 就我而言,订单始终是那个。你有其他方法吗?谢谢
    • @rob.m 我不同意这个答案,所以我不会修复它。我提交了一个替代答案,尽管此时您已经接受了这个答案-我不想迎合声誉点,所以我不会要求您接受我的,但是为了您自己,我会考虑在这个上实现它。
    • @TylerRoper tbh,我删除了接受的标记,因为我没有注意到最后一个字母错误,我看不到你的答案,它可能会更好,我希望它在哪里?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多