Javascript检查url param是否存在

时间:2016-06-13 18:25:23

标签: javascript ab-testing vwo

我正在进行A / B测试,看看是否有更多项目更适合转换。但似乎代码有时会导致错误..但我找不到任何错误,也不知道它们何时发生。

在我的测试中,我检查url param 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进行测试。

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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

如果找到,则网址参数中的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

答案 2 :(得分:0)

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

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