【问题标题】:Javascript check if url param existsJavascript 检查 url 参数是否存在
【发布时间】:2022-05-06 15:03:45
【问题描述】:

我正在运行 A/B 测试,看看显示更多商品是否更利于转化。但似乎代码有时会导致错误。但我找不到任何错误,也不知道它们何时发生。

在我的测试中,我检查 url 参数 IC 是否存在,如果不存在,我将添加它。

这是我的代码:

function checkIfAlreadyPaginated()
  {
        var field = 'IC';
    var url = window.location.href;
    if(url.indexOf('?' + field + '=') != -1)
        return true;
    else if(url.indexOf('&' + field + '=') != -1)
        return true;
    return false;
  }
function insertParam(key, value) {
        key = encodeURIComponent (key); value = encodeURIComponent (value);

        var kvp = document.location.search.substr(1).split('&');
        if (kvp == '') {
            return '?' + key + '=' + value;
        }
        else {

            var i = kvp.length; var x; while (i--) {
                x = kvp[i].split('=');

                if (x[0] == key) {
                    x[1] = value;
                    kvp[i] = x.join('=');
                    break;
                }
            }

            if (i < 0) { kvp[kvp.length] = [key, value].join('='); }

            return '?'+kvp.join('&');
        }
    }
var itemsPerPage = 48;
if(!checkIfAlreadyPaginated())
    {
      document.location.search = insertParam('IC', itemsPerPage);
    }

有人发现可能的问题吗?我正在通过 VWO.com 运行测试。

【问题讨论】:

  • 那么您看到的错误是什么?
  • 我没有看到任何错误,我认为有错误的原因是因为 vwo.com 中的流量分配不均,控制版本 - 没有此代码 - 似乎获得了两倍的流量并且vwo.com支持说原因应该是javascript错误

标签: javascript ab-testing vwo


【解决方案1】:

如果有 Javascript 错误,您应该在浏览器控制台中看到它并与我们分享。

无论如何,我都会先创建一个 JS 对象。我发现它更容易使用。

在下面的代码中,我添加了检查查询字符串的多个参数的选项。如果您只需要检查IC,您可以稍微简化一下。我在空白的 test.html 上对其进行了测试。

<script type="text/javascript">
// get the current params of the querystring
var querystringItems = document.location.search.substr(1).split('&');

// create an object
var querystringObject = {};
for(i=0;i<querystringItems.length;++i) {
    param = querystringItems[i].split('=');
    querystringObject[param[0]] = param[1];
}

// Define the keys to be searched for and their default value when they are not present
var requiredKeys = {"IC":48, "test": "me"};

// Do the checking on the querystringObject for each requiredKeys
var doreload = false;
for (var key in requiredKeys) {
    if (typeof querystringObject[key] == 'undefined') {
        doreload = true;
        // Create the missing parameter and assign the default value
        querystringObject[key] = requiredKeys[key];
    }
}

// If any of the requiredKeys was missing ...
if (doreload) {
    // rebuild the querystring
    var querystring = '?';
    for (var key in querystringObject) {
        querystring+=key+'='+querystringObject[key]+'&';
    }
    querystring=querystring.substr(0,querystring.length-1);
    // reload page
    document.location.search = querystring;
}

// assign the values to javascript variables (assuming you had it like this because you needed it)
var itemsPerPage = querystringObject.IC;
</script>

【讨论】:

    【解决方案2】:

    这是一个检查这个的例子:

    //get URL params into string:
    paramStr = window.location.substring(window.location.indexOf('?'), window.location.length;
    //turn string into array
    paramArray = paramStr.split('&');
    //prepare final array of params
    params = {};
    //prepare the index of IC parameter
    icLoc = -1; //this is negative 1 so that you know if it was found or not
    //for each item in array
    for(var i in paramArray){
        //push its name and value to the final array
        params.push(paramArray[i].split('='));
        //if the parameter name is IC, output its location in array
        if(params[i][0] === 'IC'){
            icLoc = i;
        }
    }
    

    如果没有找到 IC,icLoc 将为 -1

    如果找到,则URL参数中IC的值为params[icLoc][1]


    查询字符串?foo=bar&amp;code=cool&amp;IC=HelloWorld 的示例结果:
    params = {'foo': 'bar', 'code': 'cool', 'IC': 'HelloWorld'}
    icLoc = 2
    


    查询字符串?foo=bar&amp;code=cool 的示例:
    params = {'foo': 'bar', 'code': 'cool'}
    icLoc = -1
    

    【讨论】:

      【解决方案3】:

      这里的 id 是我用于测试的参数。传递你要检查它是否存在的参数。

      function queryParamExistUrl(param = '') {
                  if (new URLSearchParams(window.location.search).get(param) != null)
                      return true
                  return false
              }
              console.log(queryParamExistUrl('id'))

      【讨论】:

        猜你喜欢
        • 2011-07-18
        • 2019-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-09
        • 1970-01-01
        相关资源
        最近更新 更多