Javascript 检查 url 参数是否存在

问题描述 投票:0回答:3

我正在运行 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 进行测试。

javascript ab-testing vwo
3个回答
0
投票

如果有 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>

0
投票

这里有一个例子来检查这个:

//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&code=cool&IC=HelloWorld

params = {'foo': 'bar', 'code': 'cool', 'IC': 'HelloWorld'}
icLoc = 2


查询字符串示例
?foo=bar&code=cool

params = {'foo': 'bar', 'code': 'cool'}
icLoc = -1

0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.